Getting at $_GET?

Here’s my scenario. I have a contact-me page with a contact form who’s action is a PHP script that fires off an email via Mandrill. That all works fine.

When the call to Mandrill in the PHP script succeeds, I want to send the user back to the contact-me page with the GET method carrying the result variable - success in this example. So http://www.mysite.com/contact-me?result=sucess. And I think that is working too.

Here’s the problem, when I get back to the contact-me page I want to look for the result variable and not render the contact form, but instead render a success message. You guys have seen this many times on the web.

But with Grav and Twig, how do I get at that $_GET variable so I can find the contents of results?

You can use Ajax for sending the form for contact? With ajax it’s easy to receive a reply from server and take some action after successfully sending :slight_smile:

AJAX is a possibility. But I’m not that good with it, nothing else on the site uses it (yet), and I’m thinking there should be a simple way to get there with HTML and Twig. Maybe not?

Grav is intentionally kept light-weight, it does not contain a full framework under the covers as these add overhead and would slow things down. There is some basic access to parts of the request in the URI object. Anything else you should add with a plugin. You can definitely do what your asking with a plugin because we have already done similar in the admin plugin.

I guess it’s time for me to learn more about plugins. :slight_smile: Off to the docs…

Digging in here it looks as if I want to have my plugin listen for the onTwigSiteVariables event to set Twig variables for what I collect from $_GET in the plugin. Since $_GET is a super global, I assume my plugin will have access to it in the normal course of PHP?

I plan to create an array of what’s in $_GET and send that array to Twig as getArray.

Grav sends the $twig_vars array to Twig in twig.php in the processSite function. Do I have access in my plugin to that variable so that I can add my new variables to it to be passed to Twig?

You of course have full PHP access in the plugins.

Rather than accessing $_GET directly, you should use filter_input() - http://php.net/manual/en/function.filter-input.php as it’s much safer.

Have a look at the breadcrumbs Grav plugin, specifically in the onTwigSiteVariables() method to see how to add custom twig variables.

Thanks for the tips. You have been consistently helpful and I’m learning a ton about Grav from you. :slight_smile: