Using KnpMenuBundle
Welcome to KnpMenuBundle - creating menus is fun again!
Installation
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
1
composer require knplabs/knp-menu-bundle
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Step 2: Enable the Bundle
KnpMenuBundle should be automatically enabled and configured, thanks to Flex.
If you don't use Flex, you can manually enable it, by adding the following line in your project's Kernel:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// e.g. app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
// ...
new Knp\Bundle\MenuBundle\KnpMenuBundle(),
];
// ...
}
// ...
}
Step 3: (optional) Configure the bundle
The bundle comes with a sensible default configuration, which is listed below. You can define these options if you need to change them:
1 2 3 4 5 6 7 8 9 10
# config/packages/knp_menu.yaml
knp_menu:
# use "twig: false" to disable the Twig extension and the TwigRenderer
twig:
template: KnpMenuBundle::menu.html.twig
# if true, enables the helper for PHP templates
# support for templating is deprecated, it will be removed in next major version
templating: false
# the renderer to use, list is also available by default
default_renderer: twig
Note
Take care to change the default renderer if you disable the Twig support.
Create your first menu!
There are two ways to create a menu: the "easy" way, and the more flexible method of creating a menu as a service.
Method a) A menu builder as a service
For information on how to register a menu builder as a service, read Creating Menu Builders as Services.
Method b) A menu as a service
For information on how to register a service and tag it as a menu, read Creating Menus as Services.
Method c) A menu discovered by convention
For information on how to use menu based on the bundle alias convention, read Creating Menu via Naming Convention.
Note
To improve performances, you can disable providers you don't need.
Rendering Menus
Once you've set up your menu, rendering it is easy.
1
{{ knp_menu_render('my_main_menu') }}
Additionally, you can pass some options to the renderer:
1
{{ knp_menu_render('my_main_menu', {'depth': 2, 'currentAsLink': false}) }}
For a full list of options, see the "Other rendering options" header on the KnpMenu documentation.
You can also "get" a menu, which you can use to render later:
1 2
{% set menuItem = knp_menu_get('my_main_menu') %}
{{ knp_menu_render(menuItem) }}
If you want to only retrieve a certain branch of the menu, you can do the following, where 'Contact' is one of the root menu items and has children beneath it.
1 2
{% set menuItem = knp_menu_get('my_main_menu', ['Contact']) %}
{{ knp_menu_render(['my_main_menu', 'Contact']) }}
If you want to pass some options to the builder, you can use the third parameter
of the knp_menu_get
function:
1 2
{% set menuItem = knp_menu_get('my_main_menu', [], {'some_option': 'my_value'}) %}
{{ knp_menu_render(menuItem) }}