Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Route Parameters in twig Template

Tags:

routes

symfony

How can I access route parameters in a twig template without knowing the name of the parameter/s ?

like image 657
raphidue Avatar asked Jan 07 '13 13:01

raphidue


2 Answers

You can get all route parameters with:

{{ app.request.attributes.get('_route_params') }}

if you want just one parameter:

{{ app.request.attributes.get('_route_params')['YOUR_PARAMETER_KEY'] }}
like image 71
PierrickM Avatar answered Oct 26 '22 07:10

PierrickM


Route parameters can be accessed in the following way in Twig:

{{ app.request.attributes }}

You can also use the dump() function to see what methods are available:

<pre>
{{ dump(app.request.attributes) }}
</pre>

Here's a dump of all the parameters:

Requesting URL

http://example.com/test/3

Route = test
Slug = {param1} = 3

Twig Code

{{ dump(app.request.attributes) }}

Returns

object(Symfony\Component\HttpFoundation\ParameterBag)[10]
  protected 'parameters' => 
    array (size=3)
      '_controller' => string 'MyTest\Bundle\Controller\TestController::indexAction' (length=61)
      'param1' => string '3' (length=1)
      '_route' => string 'test' (length=7)
like image 27
phpisuber01 Avatar answered Oct 26 '22 07:10

phpisuber01