NaGeL182 NaGeL182 - 3 months ago 21
PHP Question

Symfony2 KnpMenuBundle: set active a menu item even when its not on that menu

I created my menu builder and it works.
One of my route is

/database


But this has a child route:

database/view/{id}


I don't want to put the view route into the menu items because without the ID it won't work.

But I want the database route to be active when the user is on the view.

How can I do this?

Answer

Managed to solve it with this little hack:

in the menuBuider after you add all the Children but before you return the menu i added

$request = $this->container->get('request');
        $routeName = $request->get('_route');
        switch ($routeName)
        {
            case 'battlemamono_database_view_by_name':
            case 'battlemamono_database_view_by_id':
                $database->setCurrent(true);
                break;
        }

this checks the routes and sets the needed menu active.

Comments