0

I am new to Symfony, and trying to follow the instructions at https://symfony.com/doc/current/KnpMenuBundle/index.html to set up a navigation menu the "easy" way. It says to "first create a new class in the Menu directory of one of your bundles." I thought bundles were things that I install with composer. I don't know that I have any bundles of my own, as such. The docs go on to say that an example builder class would look like

    // src/Menu/Builder.php [ <-- comment in the original ]

    namespace App\Menu;
    
    use Knp\Menu\FactoryInterface;
    use Knp\Menu\ItemInterface;
    // [etc]

    final class Builder
    {
       
        public function mainMenu(FactoryInterface $factory, array $options): ItemInterface
        {
            $menu = $factory->createItem('root');

            $menu->addChild('Home', ['route' => 'homepage']);
            // [etc]
        
            // create another menu item
            $menu->addChild('About Me', ['route' => 'about']);
            // you can also add sub levels to your menus as follows
            $menu['About Me']->addChild('Edit profile', ['route' => 'edit_profile']);

            // ... add more children

            return $menu;
        }
    }

so I put this file in src/Menu/Builder.php and try to render it, as shown in the documentation, in my base.html.twig template by saying {{ knp_menu_render('App:Builder:mainMenu') }}. The result is a RuntimeError:

An exception has been thrown during the rendering of a template ("Bundle "App" does not exist or it is not enabled. Maybe you forgot to add it in the "registerBundles()" method of your "App\Kernel.php" file?").

I'm quite sure the KnpMenuBundle is registered in my config/bundles.php, but I don't know how to solve the above error about the "Bundle 'App'"

(I've also tried registering it as a service, per the instructions, but that didn't work either. I didn't save the error information produced by that effort, but I will repeat the steps and post it if anyone thinks it would be useful.)

I could use some help understanding where I'm going wrong. Thanks.

David
  • 815
  • 8
  • 18
  • 1
    That's **very** outdated documentation back from the symfony 3.x days where [your application itself](https://symfony.com/blog/new-in-symfony-3-4-deprecated-bundle-inheritance) would be structured in bundles and didn't refer only to external dependencies, so the [preferred method now](https://github.com/KnpLabs/KnpMenu/issues/283#issuecomment-348910927) is to [register menu builders as services](https://symfony.com/bundles/KnpMenuBundle/current/menu_builder_service.html). That method _should_ work as written, otherwise, [edit] your question and add more details. – msg Feb 05 '22 at 17:04
  • @msg ok thank you! I got tired of struggling and implemented my own simple solution: iterating over an array of menu items (no nesting/sub-menus supported) stored as Twig globals. When I get the chance I will give this another go. – David Feb 06 '22 at 21:39

0 Answers0