Tom Tom - 13 days ago 9
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.

app.request.get('_route')


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

controller method takes 2 optional arguments. http://symfony.com/doc/current/reference/twig_reference.html#controller

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')
  }
)) }}