Is there a way to replace a GET parameter value from twig?
For example, I have a page at this address:
http://localhost/app_dev.php/test/?param1=40&sort=name
And in my twig 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
For now I added the "&sort" parameter once again at the end on 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 only have 2 parameters, but in reality I have around 6 parameters, because the link that's generated it's obtained by submitting a .
This should solve your problem:
{{ path(app.request.attributes.get('_route'),
app.request.query.all|merge({'sort': 'address'})) }}
It gets the current route and all query parameters which are merged with the one you like to update before they are appended.
Symfony/Twig path
function accept optional params. If these params are part of the route, they're handled by router but if they're not, they are passed as GET parameters.
So, if your corresponding route is, for example, my_route
:
<a href="{{ path('my_route', {'param1':40, 'sort':'address'}) }}">address</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With