4
votes

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.

4

4 Answers

17
votes

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

{{ app.request.requesturi }}
6
votes

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})) }}
0
votes

Are different actions. Different actions that have different templates. With twig you can have 2 or 3 templates that extends a third one. In third one you can define the bloc of the list. And a blok of the form. In twig you can extends templates.

I still, ... think you need different templates and different actions. Each action's template, extends a "super" template with the list.

-1
votes

You can use multiple @Route in you action:

/**
  * @Route( "/route/{id}", name="_route" )
  * @Route( "/route/{id}/hello", name="_route_hello" )
  * @Route( "/route/{id}/hello/{world}", name="_route_hello_world" )
  * @Template()
  */

And check if a variable exists ...

But ... I do not understand you need to use same action with different route. I prefer use one action for each "purpose". And to follow DRY pattern, I like to write some private method for not rewrite code ...