Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 + Twig get real/full current route

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.

like image 768
insertusernamehere Avatar asked Nov 29 '22 02:11

insertusernamehere


2 Answers

The Request class has a getRequestUri() method. You can access it in twig like

{{ app.request.requesturi }}
like image 154
Maerlyn Avatar answered Dec 06 '22 01:12

Maerlyn


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})) }}
like image 38
Denis V Avatar answered Dec 06 '22 00:12

Denis V