简体   繁体   English

Symfony2 - 在twig partial / subrequest中获取主要请求的当前路由

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

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. 在由独立控制器呈现的Twig部分呈现中,我想检查当前主路由是否等于比较路由,因此我可以将列表项标记为活动。

How can I do that? 我怎样才能做到这一点? Trying to get current route in BarController like: 试图在BarController中获取当前路线,如:

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

returns null . 返回null

Uri is also not what I'm looking for, as calling below code in bar 's twig: Uri也不是我想要的,因为在bar的树枝上调用下面的代码:

app.request.uri

returns route similar to: localhost/_fragment?path=path_to_bar_route 返回类似于: localhost/_fragment?path=path_to_bar_route路由

Full example 完整的例子

Main Controller: FooController extends Controller{ 主控制器:FooController扩展Controller {

    public function fooAction(){}
}

fooAction twig: fooAction树枝:

...some stuff...

{{ render(controller('FooBundle:Bar:bar')) }}

...some stuff...

Bar controller: 酒吧控制器:

BarController extends Controller{

    public function barAction(){}
}

barAction twig: barAction树枝:

<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, the original problem occurs probably because of the request_stack . 但是,原始问题可能是因为request_stack

http://symfony.com/blog/new-in-symfony-2-4-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 . 由于您处于子请求中,因此您应该能够获得顶级( masterRequest并获取_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... 没有运行它但它应该工作...

I think that the best solution in your case is past the current main route in the render: 我认为在你的情况下,最好的解决方案是通过渲染中的当前主路径:

{{ render(controller('FooBundle:Bar:bar', {'current_route' : app.request.uri})) }}

Next, return it in the response: 接下来,在响应中返回它:

public function barAction(Request $request) {
    ...
    return $this->render('Template', array('current_route' => $request->query->get('current_route'));
}

And in your template compares with the received value. 并在您的模板中与收到的值进行比较。

Otherwise, maybe is better to use a include instead a render, if you don't need extra logic for the partial. 否则,如果你不需要额外的部分逻辑,也许最好使用include而不是render。

in twig you can send request object from main controller to sub-controller as parameter: 在twig中你可以将请求对象从主控制器发送到子控制器作为参数:

{{ render(controller('FooBundle:Bar:bar', {'request' : app.request})) }}

in sub-controller: 在子控制器中:

BarController extends Controller{

    public function barAction(Request $request){
// here you can use request object as regular
$country = $request->attributes->get('route_country');
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM