2016-03-08 64 views
0

我试图在我的动作中获取当前路线模式,因为您可以在debug:router命令中获得。当前路线模式

all_medias  GET  ANY  ANY /api/media/all.{_format} 

我看了在RouterDebugCommand,但你需要一个$input$ouput对象使用助手。这似乎有点过于复杂的任务。有没有办法从控制器获取路由模式?

回答

1

您可以按名称从RouteCollection

获取路线
$currentRouteName = $request->attributes->get('_route'); 
$currentRoutePath = $this->get('router')->getRouteCollection()->get($currentRouteName)->getPath(); 

或者你也可以检查其模式(自2.2 deprated):

$currentRoutePattern = $this->get('router')->getRouteCollection()->get($currentRouteName)->getPattern();