2015-01-07 29 views
1

这是我的路线Symfony2的阅读路径参数

personeelslid_personeelslid_detail: 
pattern: /personeelslid/detail/{personeelId} 
defaults: 
    _controller: PersoneelPersoneelslidBundle:Personeelslid:detail 
    _menu_icon: fa-user 

locatiebeheer_kind_detail: 
pattern: /locatiebeheer/kind/detail/{kindId} 
defaults: { _controller: PersoneelLocatiemanagerBundle:KindDetail:detail } 

在我使用MenuBuilder我遍历所有路线和获取例如_menu_icon:

$routeObject = $this->_router->getRouteCollection()->get($route);    

$menu_icon = $routeObject->getDefault('_menu_icon'); 

我如何可以获取变量名personeelId,kindId, 我需要te键,所以我可以设置routeParameter属性。

回答

2

如果需要,您可以将自定义键添加到defaults选项。例如:

personeelslid_personeelslid_detail: 
path: /personeelslid/detail/{personeelId} 
defaults: 
    _controller: PersoneelPersoneelslidBundle:Personeelslid:detail 
    _menu_icon: fa-user 
    my_custom_key: personeelId 

,并得到它:

$myCustomKey = $routeObject->getDefault('my_custom_key'); 

如果你想获取通配符的值,你不能”做这样的方式(通过getRouteCollection),从Request对象获取它(如$request->attributes->all())..

+0

谢谢,我知道这个选项。希望有更优雅的方式。那么我会这样做。 – Bart

+1

@Bart我还没有看到更好的选择,只有这种方式或PHP的'explode'函数和正则表达式与'getRouteCollection' :) – xurshid29