Symfony2 / Twig dealing with getting URL query string values

user2143356 picture user2143356 · Mar 25, 2013 · Viewed 38.3k times · Source

I'm trying to manipulate the query string values in a URL.

I can get the current URL or route either from the Request object or Twig's functions, but it's the query string I'm struggling with.

I don't need app.request.attributes.get('_route_params') as this gets the query string params that are in the route.

I need to get query string params that are actually in the URL.

I want to be able to do the two things listed below in both Symfony2 (in a PHP controller) and Twig (in a Twig template):

  1. Get all current query string values in the URL and display them

  2. Do 1, but change one of the query string values before displaying them

I can't find anyone who knows how to do this.

Answer

Thomas Potaire picture Thomas Potaire · Mar 25, 2013

You can use app.request.query.all to get your query strings.

If you want to change a param in Twig you can do this

{% set queryParams = app.request.query.all %}
{% set queryParams = queryParams|merge({queryKey: newQueryValue}) %}