Skip to content
niknetniko edited this page Mar 5, 2016 · 4 revisions

Creating a new menu collection is simple. Simply use the make() method:

...

Menu::make('example', function(Builder $menu) {
    // You may define and add your menu links here
});

...

The above will create a new "example" menu collection, and register a new global view composer by the name of $menu_example that you may call within your view files to render your menu.


Some of you may ask were the best place to house the code to create your menu collections. A middleware would be the ideal place, but you may opt to simply create your menus within your routes file as well if you're developing a very small application.

Here's a middleware stub to get you started. Just remember to register it within your applications app config file.

<?php

namespace App\Http\Middleware

use Closure;
use Menu;
use Caffeinated\Menus\Builder;

class MenuMiddleware
{
    /**
     * Run the request filter.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure                  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        Menu::make('example', function(Builder $menu) {
            $menu->add('Home', '/');
            $menu->add('About', '/about');
            $menu->add('Blog', '/blog');
            $menu->add('Contact Me', '/contact-me');
        });

        return $next($request);
    }
}
Clone this wiki locally