简体   繁体   中英

Symfony2 read route parameter

This is my route

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 }

In my menuBuilder I loop through all routes and fetch for example _menu_icon:

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

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

How can I fetch the variable name personeelId, kindId, etc.. I need te keys so I can set the routeParameter attributes.

You can add custom keys to defaults option if you want. Example:

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

and get it:

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

IF you want to fetch the value of wildcard, you cant' do it like this way (by getRouteCollection ), fetch it from Request object (eg $request->attributes->all() )..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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