3

Let's say I have a routing setup like that :

users_list:
    pattern:  /users
    defaults: { _controller: AcmeBundle:User:list }

user_edit:
    pattern:  /user-edit/{id}
    defaults: { _controller: AcmeBundle:User:edit }

I would like to setup a menu like this

  • Home
  • --Users list
  • ----User edition

But I'm not sure how to handle my route 'user_edit' with dynamic params "id". Actually I don't want to display the link (which id ?), but I would like the 'Users list' parent node to be active if I edit an user.

I tried something like this

$userNode = $rootNode->addChild('Users list', array(
    'route' => 'users_list',
));
$userNode->addChild('User edition', array(
    'route' => 'user_edit',
));

Symfony complains about the missing parameter :(

Thanks !

Thomas Piard
  • 1,209
  • 2
  • 15
  • 25

2 Answers2

4

This setup should work if you want to show the links:

$userNode->addChild('User edition', array(
    'route' => 'user_edit',
    'routeParameters' => array('id' => $someParameter)
));

For setting the parrent node as active you could use a custom renderer, override the menubundle's template or just add active class to the menu item based on this condition:

{% if app.request.attributes.get('_route') == 'user_edit' %}
       {# activate parrent node #}
{% endif %}
Udan
  • 5,429
  • 2
  • 28
  • 34
  • Yeah but then i create the link for a specific id, if I want the link to be "active" for any id specified I can't – Thomas Piard Oct 18 '13 at 12:45
  • app.request.attributes.get('_route') returns the name of the rout which is the same for any id used – Udan Oct 18 '13 at 12:50
  • Ok got it, so I have to make custom conditions for any "dynamic" route I have ? I'm actually trying to reproduce this for my entire backend – Thomas Piard Oct 18 '13 at 13:02
  • Hi @ThomasPiard interested to know if you found an elegant solution here? – benrcole Dec 23 '16 at 17:23
4

Found something usefull in my case, you're able to solve this using the request in your menu definition class :

$userNode->addChild('User edition', array(
    'route' => 'user_edit',
    'routeParameters' => array('id' => $this->getRequest()->get('id'))
));
Thomas Piard
  • 1,209
  • 2
  • 15
  • 25