Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Archive

Getting at $_GET?

Started by Muut Archive 12 years ago · 7 replies · 316 views
12 years ago

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?

12 years ago

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 :)

12 years ago

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?

12 years ago

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.

12 years ago

I guess it's time for me to learn more about plugins. :) Off to the docs...

12 years ago

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?

12 years ago

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.

12 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1280 9 years ago
Archive · by Muut Archive, 9 years ago
2 888 9 years ago
Archive · by Muut Archive, 9 years ago
2 4018 9 years ago
Archive · by Muut Archive, 9 years ago
1 2893 9 years ago
Archive · by Muut Archive, 9 years ago
3 1077 9 years ago