-
Notifications
You must be signed in to change notification settings - Fork 0
Handling Menus
The main integration part is done via "dowilcox/knp-menu-laravel"
providing a ServiceProvider and a Menu Facade, see Relevant Links for more information. Its tied together with Vain in the Vain\Providers\MenuServiceProvider
like follows:
Frontend menu.frontend
$this->app->singleton('menu.frontend', function() {
return Menu::create('frontend');
});
View::composer('app', function($view) {
$handler = app('menu.frontend');
$view->with('menu', $handler);
Event::fire(new FrontendMenuCreated($handler, $view));
});
Backend menu.backend
$this->app->singleton('menu.backend', function () {
return Menu::create('backend');
});
View::composer('admin', function ($view) {
$handler = app('menu.backend');
$view->with('menu', $handler);
Event::fire(new BackendMenuCreated($handler, $view));
});
The menu should only loaded if a specific view (frontend or backend) was requested and the menu is even necessary (for ajax requests its not). Therefore the loading callback is placed in an View Composer, for more information visit the Laravel Documentation.
The menu handler is accessible at any time as a singleton from the service container by using menu.frontend
and menu.backend
instead of creating a new menu (as you will see in the original KnpMenu Docs) like this:
$factory = new MenuFactory();
$menu = $factory->createItem('My menu');
You will also see that there is an event which is fired once a menu handler is created. It is the entry point for providing your own menus from any component of the app.
Providing your own menu items is as simple as listen to the Vain\Events\FrontendMenuCreated
or Vain\Events\BackendMenuCreated
event with an event handler, for more information see Laravel Documentation. How you handle them is totally up to you.
According to those two events you can provide your menu items. See KnpMenu Docu - Basic Menus for further explanation.
Example
/**
* @return void
*/
protected function handle(BackendMenuCreated $event)
{
$event->handler->addChild('user.admin')
->setUri('#')
->setLabel('user::user.title.index')
->setExtra('icon', 'users'); // font awesome icons
$event->handler['user.admin']->addChild('user::user.title.index')
->setUri(route('user.admin.users.index'))
->setExtra('icon', 'circle-o'); // only supported with AdminLTE presenter
$event->handler['user.admin']->addChild('user::role.title.index')
->setUri(route('user.admin.roles.index'))
->setExtra('icon', 'circle-o');
$event->handler['user.admin']->addChild('user::permission.title.index')
->setUri(route('user.admin.permissions.index'))
->setExtra('icon', 'circle-o');
}
NOTE: Labels are automatically translated using laravel's trans()
method. If you do not need this just use ->setExtra('raw', true)
on the menu item of your choice.
Most of your menu items will gain their active states automatically. The given MenuItem::getUri()
is matched against the current application's url. If you encounter a situation where this behavior is not enough you can do one of the following.
$event->handler['...']->addChild('...')
->setExtra('routes', ['route.name.one', 'route.name.two'])
...
The used regex pattern will be matched against the current applications route name.
$event->handler['...']->addChild('...')
->setExtra('patterns', ['/route\.name\.(.+)/', '/route\.other\.(.+)/'])
...
All routes which match one of those will trigger the targeted item as active.
You generally do not have to care about menu presenters. They are already implemented for Bootstrap 3 and AdminLTE.
- KnpMenu Laravel on GitHub (Laravel ServiceProvider used for Integration)
- KnpMenu on GitHub (Core KnpMenu Component)
- KnpMenu Docu - Basic Menus
- KnpMenu Docu - Iterators and Manipulation
Any problems? Try our Troubleshooting page!