Accessing URL query variables directly in Symfony controllers

URL query variables are ugly, but they’re helpful.  They’re the question mark at the end of a website address that sometimes has cryptic information and often has information any user can glean a bit of information that can tell us how the website works.

For instance, type in a search in Google like “belchamber code” into Google and you will see at the top of the address bar the following:

“https://www.google.com/search?q=belchamber+code&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb”

See the letter “q” after the question mark?  It can stand for anything the web developer wants, but in this case it stands for “query”.  Notice the ampersand (&) after?  That’s the web’s way to send even more information about your current action/session.  These variables in the address bar can then be grabbed by the code and use the information in subsequent coding logic to create behaviors.  So the website does something for the user based on the “Request – Response” workflow / philosophy / paradigm.

For Symfony, access these “get” or “request” variables is as easy as doing it in straight PHP, but safer:

Belchamber Code Google results screeenshot

Belchamber Code Google results screeenshot

Aaron Belchamber has written 243 articles

Leave a Reply

Your email address will not be published. Required fields are marked *