php 如何在 Symfony 2 中获取当前路线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7096546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current route in Symfony 2?
提问by IlyaDoroshin
回答by tuxedo25
From something that is ContainerAware (like a controller):
从 ContainerAware 的东西(如控制器):
$request = $this->container->get('request');
$routeName = $request->get('_route');
回答by Matthieu
With Twig : {{ app.request.attributes.get('_route') }}
用树枝: {{ app.request.attributes.get('_route') }}
回答by supernova
I think this is the easiest way to do this:
我认为这是最简单的方法:
class MyController extends Controller
{
public function myAction($_route)
{
var_dump($_route);
}
.....
回答by jsgoupil
Symfony 2.0-2.1
Use this:
Symfony 2.0-2.1
使用这个:
$router = $this->get("router");
$route = $router->match($this->getRequest()->getPathInfo());
var_dump($route['_route']);
That one will notgive you _internal
.
那个不会给你的_internal
。
Update for Symfony 2.2+:This is not working starting Symfony 2.2+. I opened a bugand the answer was "by design". If you wish to get the route in a sub-action, you must pass it in as an argument
Symfony 2.2+ 更新:从 Symfony 2.2+ 开始这不起作用。我打开了一个错误,答案是“按设计”。如果您希望在子操作中获取路线,则必须将其作为参数传入
{{ render(controller('YourBundle:Menu:menu', { '_locale': app.request.locale, 'route': app.request.attributes.get('_route') } )) }}
And your controller:
还有你的控制器:
public function menuAction($route) { ... }
回答by K. Norbert
There is no solution that works for all use cases. If you use the $request->get('_route') method, or its variants, it will return '_internal'for cases where forwarding took place.
没有适用于所有用例的解决方案。如果您使用 $request->get('_route') 方法或其变体,它将在发生转发的情况下返回 '_internal'。
If you need a solution that works even with forwarding, you have to use the new RequestStack service, that arrived in 2.4, but this will break ESI support:
如果您需要一个甚至可以与转发一起使用的解决方案,您必须使用新的 RequestStack 服务,该服务在 2.4 中推出,但这会破坏ESI 支持:
$requestStack = $container->get('request_stack');
$masterRequest = $requestStack->getMasterRequest(); // this is the call that breaks ESI
if ($masterRequest) {
echo $masterRequest->attributes->get('_route');
}
You can make a twig extension out of this if you need it in templates.
如果您在模板中需要它,您可以使用它制作树枝扩展。
回答by Tom Tom
_route
is not the way to go and never was. It was always meant for debugging purposesaccording to Fabien who created Symfony. It is unreliable as it will not work with things like forwarding and other direct calls to controllers like partial rendering.
_route
不是要走的路,从来都不是。根据创建 Symfony 的 Fabien 的说法,它总是用于调试目的。它是不可靠的,因为它不适用于转发和其他直接调用控制器(如部分渲染)之类的事情。
You need to inject your route's name as a parameter in your controller, see the doc here
您需要在控制器中注入路由名称作为参数,请参阅此处的文档
Also, please never use $request->get('');
if you do not need the flexibility it is way slower than using get on the specific property bag that you need (attributes, query or request) so $request->attributes->get('_route');
in this case.
此外,$request->get('');
如果您不需要灵活性,请不要使用它比在您需要的特定属性包(属性、查询或请求)上使用 get 慢得多,因此$request->attributes->get('_route');
在这种情况下。
回答by Venkat Kotra
$request->attributes->get('_route');
You can get the route name from the request object from within the controller.
您可以从控制器内的请求对象中获取路由名称。
回答by alexismorin
All I'm getting from that is
_internal
我从中得到的只是
_internal
I get the route name from inside a controller with $this->getRequest()->get('_route').
Even the code tuxedo25 suggested returns _internal
我从控制器内部获取路由名称,$this->getRequest()->get('_route').
即使代码 tuxedo25 建议返回_internal
This code is executed in what was called a 'Component' in Symfony 1.X; Not a page's controller but part of a page which needs some logic.
这段代码在 Symfony 1.X 中所谓的“组件”中执行;不是页面的控制器,而是需要一些逻辑的页面的一部分。
The equivalent code in Symfony 1.X is: sfContext::getInstance()->getRouting()->getCurrentRouteName();
Symfony 1.X 中的等效代码是: sfContext::getInstance()->getRouting()->getCurrentRouteName();
回答by Muhammad Shahzad
With Symfony 3.3, I have used this method and working fine.
在 Symfony 3.3 中,我使用了这种方法并且工作正常。
I have 4 routes like
我有 4 条路线,例如
admin_category_index, admin_category_detail, admin_category_create, admin_category_update
admin_category_index、admin_category_detail、admin_category_create、admin_category_update
And just one line make an active class for all routes.
并且只有一行为所有路由创建一个活动类。
<li {% if app.request.get('_route') starts with 'admin_category' %} class="active"{% endif %}>
<a href="{{ path('admin_category_index') }}">Product Categtheitroades</a>
</li>
回答by Alain Tiemblo
To get the current route based on the URL (more reliable in case of forwards):
要根据 URL 获取当前路由(在转发的情况下更可靠):
public function getCurrentRoute(Request $request)
{
$pathInfo = $request->getPathInfo();
$routeParams = $this->router->match($pathInfo);
$routeName = $routeParams['_route'];
if (substr($routeName, 0, 1) === '_') {
return;
}
unset($routeParams['_route']);
$data = [
'name' => $routeName,
'params' => $routeParams,
];
return $data;
}