Symfony KnpMenuBundle route matching for similar-routes

152 views Asked by At

Lets say I have a collapsible menu, where one entry in the collapsible part is mapped to the route:

test/index

How can I achieve that the the collapsible menu stays open and the entry inside is marked as the active one if I change the site location to lets say:

test/create / test/update / test/detail etc.

1

There are 1 answers

0
spackmat On

In your MenuBuilder you can match those routes with a switch and call setCurrent() on the item you want to be current. For example:

// The MenuBuilder must have access to the container
$request = $this->container->get('request');
switch ($request->get('_route'))
{
    case 'route_create':
    case 'route_update':
        $menu->getChild('Index')->setCurrent(true);
        break;
}

This will set the MenuItem 'Index' to current, if 'route_create' or 'route_update' is the current route.

Another appoach could be to match all routes as you normally did, then call setDisplay(false) on them to hide them. I didn't try this, but I think this should work, too.