Access route parameters in the branch template

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

+8
source share
3 answers

Access to route parameters can be obtained as follows in Twig:

{{ app.request.attributes }} 

You can also use the dump() function to find out which methods are available:

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

Here is a dump of all parameters:

URL request

 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) 
+15
source

You can get all route parameters with:

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

if you want only one parameter:

 {{ app.request.attributes.get('_route_params')['YOUR_PARAMETER_KEY'] }} 
+11
source

if you want to use the current route with its parameter as a URL:

{{path (app.request.get ('_ route'), app.request.get ('_ route_params'))}}

This can be useful if you want to remove any related strings from the URL.

 app.request.get('_route') gives you the route name from request bag. app.request.get('_route_params') gives you the route parameters {{ path(route_name, array of parameters) }} create the path 
0
source

All Articles