I have a routing with optional parameters:
/**
* @Route( "/route/{id}", name="_route", defaults={"id" = ""} )
* @Template()
*/
In the template I have a form and I want the form to be send to either:
/route
or:
/route/10
/route/10/mail – if there were more than just one parameter
At the moment I'm solving it like this:
{{ path(app.request.attributes.get('_route')) }}/{{ object.id }}
Which works fine, but I have to add all possible parameters by myself. Is there a way to get the full current path from within twig? I don't want to add an extra variable in my controller to be send to the template.
The Request
class has a getRequestUri()
method. You can access it in twig like
{{ app.request.requesturi }}
There is one more way (not sure whether it is a good practice or not):
{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}
And in this case you can add an additional parameter to it:
{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({'p': 10})) }}
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