How can I access route parameters in a twig template without knowing the name of the parameter/s ?
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'] }}
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)
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