Because we want a modular system we are using events to build the menu. When the menu is build an event called "framework_core.configure_menu" is triggered.
Your bundle can subscribe on this event. Follow the steps below to implement it in your own bundle
<?php
namespace SumoCoders\FrameworkExampleBundle\EventListener;
use SumoCoders\FrameworkCoreBundle\Event\ConfigureMenuEvent;
use SumoCoders\FrameworkCoreBundle\EventListener\DefaultMenuListener;
class MenuListener extends DefaultMenuListener
{
public function onConfigureMenu(ConfigureMenuEvent $event)
{
$menu = $event->getMenu();
// if you need to add a menu with subitems you can use a shortcut like this
$childs = [
'this.is.the.label' => 'sumocoders_frameworkexample_route_name',
'another.label' => $event->getFactory()->createItem(
'another.label',
[
'route' => 'sumocoders_frameworkexample_default_hello',
'routeParameters' => [
'name' => 'John Doe',
],
]
),
];
$menuItem = $this->createItemWithChilds(
$event->getFactory(),
'a.label',
1000,
$childs
);
$menu->addChild($menuItem);
$menuItem = $event->getFactory()->createItem(
'example.menu.overview',
[
'route' => 'sumocoders_frameworkexample_route_name',
]
);
}
}
Add the configuration
services:
example_menu_listener:
class: SumoCoders\FrameworkExampleBundle\EventListener\MenuListener
arguments:
- "@security.authorization_checker"
- "@security.token_storage"
tags:
- { name: kernel.event_listener, event: framework_core.configure_menu, method: onConfigureMenu }
You can use normal numbers such as 1, 2, 3. But I would advice that you use hundreds, as it will allow you to insert items in between in a later stage.