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

前端 未结 3 751
名媛妹妹
名媛妹妹 2020-12-31 18:46

In Twig partial rendered by separate controller, I want to check if current main route equals to compared route, so I can mark list item as active.

How can I do tha

3条回答
  •  情话喂你
    2020-12-31 19:15

    pabgaran's solution should work. However, the original problem occurs probably because of the request_stack.

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

    Since you are in a subrequest, you should be able to get top-level (master) Request and get _route. Something like 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 this but it should work...

提交回复
热议问题