2

In a Symfony 5.0 Application I want to use the KnpMenuBundle. I installed it by running

composer require knplabs/knp-menu-bundle "^3.0"

This automatically created an entry in [ProjectRoot]/config/bundles.php":

Knp\Bundle\MenuBundle\KnpMenuBundle::class => ['all' => true]

Of course it downloaded the bundle as well.

The documentation (found at: https://symfony.com/doc/master/bundles/KnpMenuBundle/index.html) states to configure the bundle one should edit the file "app/config/config.yml". Symfony 5 does not have this file anymore - but single config files for each "package" in "[ProjectRoot]/config/packages". However there wasn't a config file created for KnpMenuBundle. How could I do this myself - meaning: Whats the naming conventions and how do I tell the bundle to actualy use the config file?

There is another issue. In the documentation it says rendering a Menu in a twig template works like this:

{{ knp_menu_render('AppBundle:Builder:mainMenu') }}

For Symfony 5 - what would be the right syntax / path there?

user3440145
  • 793
  • 10
  • 34

1 Answers1

2

Configuration for KnpMenuBundle is optional. That is why there is no configuration file created. In Symfony 4 and 5 you can just add a yaml config file in the config/packages directory manually. It will be automatically read. A good name for this file could be knp_menu.yaml. Inside the config file you use the same content as documented. e.g.

knp_menu:
    twig:
        template: KnpMenuBundle::menu.html.twig

To render the menu you can use

{{ knp_menu_render('App:Builder:mainMenu') }}

Where Builder is the name of the class inside the src/Menu directory and mainMenu is method to call from the class (See the docs).

UPDATE

If i am not wrong you have to define the Builder class as a service from Symfony 4 which is slightly different. As you can read from the docs you will now render your menu giving the alias of the service e.g.

{{ knp_menu_render('main') }}

(Change all AppBundle occurrences to App).

Frank B
  • 3,667
  • 1
  • 16
  • 22
  • In the linked doc there are 3 methods described to use the menu. I chose method a). But trying to render with {{ knp_menu_render('App:Builder:mainMenu') }} gets me this error: 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?"). – user3440145 Jan 16 '20 at 16:00
  • But you also said Symfony 4 / 5 require me to use the Builder Class service way - so I probably just have to switch to that. – user3440145 Jan 16 '20 at 16:01
  • Properly yes. It worked for me a while ago on Symfony 4. – Frank B Jan 17 '20 at 01:38
  • So - I can't say for sure if it can't work with method a) - however it DOES work with method b) which is the menu builder way. So thanks for that. – user3440145 Jan 17 '20 at 14:55
  • 1
    It does look like your suggestion how to provide a config-file works too - as it gives an error as soon as I use it like you said. But it says: "Unable to find template "KnpMenuBundle::menu.html.twig" (looked into: ...)" Trying to load "knp_menu.html.twig" which definitely exists in the vendors path where it looks doesn't change anything. Any idea what the issue is here? – user3440145 Jan 17 '20 at 14:58
  • 1
    OK - the config file works like you suggest. However - I can't figure out how to change your example to actualy work. It just does not find the template using this notation. What would be the right way of writing this? – user3440145 Jan 17 '20 at 16:28