Tom Tom - 9 months ago 111
PHP Question

Symfony2 app.request.get return null in rendered twig template

I render my header.twig from the base.twig file via the render function. So in my base.twig there is the following code to trigger header controller:

{{ render(controller('MyBundle:Global:header')) }}

That controller renders the header.twig. In this twig file there the the following code link for changing the language:

<a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : app.request.locale })) }}"><img src="{{ asset('flags/'~app.request.locale~'.png', 'img') }}" /></a>

The objects form app.request.get('_route') and app.request.get('_route_params') both return null.


If I run the same code link directly in the base.twig the request returns the correct objects. Looks like because the header.twig has it own controller the request are not working. Is it possible to request the route and parametsr of the active url in a other way?

Answer Source

controller method takes 2 optional arguments.

Not 100% sure about it but maybe this will work:

{{ render(controller('MyBundle:Global:header',
    '_route': app.request.get('_route'),
    '_route_params': app.request.get('_route_params')
)) }}