Skip to content
This repository has been archived by the owner on Jan 25, 2020. It is now read-only.

Handling Menus

Voydz edited this page Apr 6, 2015 · 8 revisions

Handling Menus

Table of Contents

  1. Integration
  2. Providing Menu Items
  3. Active States
  4. Presenters
  5. Relevant Links

Integration

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 Menu Items

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.

Active States

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.

Additional route names

$event->handler['...']->addChild('...')
    ->setExtra('routes', ['route.name.one', 'route.name.two'])
    ...

Regex pattern

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.

Presenters

You generally do not have to care about menu presenters. They are already implemented for Bootstrap 3 and AdminLTE.

Relevant Links