php - Symfony2 - get main request's current route in twig partial/subrequest -


in twig partial rendered separate controller, want check if current main route equals compared route, can mark list item active.

how can that? trying current route in barcontroller like:

$route = $request->get('_route'); 

returns null.

uri not i'm looking for, calling below code in bar's twig:

app.request.uri 

returns route similar to: localhost/_fragment?path=path_to_bar_route

full example

main controller: foocontroller extends controller{

    public function fooaction(){} } 

fooaction twig:

...some stuff...  {{ render(controller('foobundle:bar:bar')) }}  ...some stuff... 

bar controller:

barcontroller extends controller{      public function baraction(){} } 

baraction twig:

<ul>        <li class="{{ (item1route == currentroute) ? 'active' : ''}}">         item 1     </li>     <li class="{{ (item2route == currentroute) ? 'active' : ''}}">         item 2     </li>     <li class="{{ (item3route == currentroute) ? 'active' : ''}}">         item 3     </li>   </ul> 

pabgaran's solution should work. however, original problem occurs because of request_stack.

http://symfony.com/blog/new-in-symfony-2-4-the-request-stack

since in subrequest, should able top-level (master) request , _route. this:

public function baraction(request $request) {     $stack = $this->get('request_stack');     $masterrequest = $stack->getmasterrequest();     $currentroute = $masterrequest->get('_route');      ...     return $this->render('template', array('current_route' => $currentroute ); } 

haven't run should work...


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -