Twig - dynamically replace the value of the GET parameter

Is there a way to replace the value of the GET parameter from a branch?

For example, I have a page at this address:

http://localhost/app_dev.php/test/?param1=40&sort=name

And in my branch I want to build 3 links like this:

http://localhost/app_dev.php/test/?param1=40&sort=name 
http://localhost/app_dev.php/test/?param1=40&sort=address 
http://localhost/app_dev.php/test/?param1=40&sort=code 

Now I have added the "& sort" parameter again at the end of the URL, but this solution is actually a patch and it sucks!

<a href="{{app.request.requesturi}}&sort=address">address</a>

In this example, I have only 2 parameters, but in fact I have about 6 parameters, because the link that generated it was obtained by sending.

+5
source share
2 answers

This should solve your problem:

{{ path(app.request.attributes.get('_route'),
   app.request.query.all|merge({'sort': 'address'})) }}

It receives the current route and all request parameters that are combined with the one you want to update before they are added.

+22

Symfony/Twig path . , , , GET.

, , , my_route:

<a href="{{ path('my_route', {'param1':40, 'sort':'address'}) }}">address</a>
+2

All Articles