Welcome to NukaCode Menu package

This package is designed to manage menus for your application.

Badges

https://scrutinizer-ci.com/g/NukaCode/menu/badges/quality-score.png?b=master https://scrutinizer-ci.com/g/NukaCode/menu/badges/coverage.png?b=master https://travis-ci.org/NukaCode/menu.svg?branch=master

Guides

Installation

Installation guide for the NukeCode Menu package. This package requires Laravel 5.0 or higher.

Composer

Run the following command to require the menu package in your project.

composer require nukacode/menu:1.0.*
Service Provider

Add the following to the service providers in config/app.php.

'NukaCode\Menu\MenuServiceProvider',
Middleware

Add the following to the kernel in app\kernel.php

'menu' => '\NukaCode\Menu\Middleware\MenuMiddleware',

Quick Start

This is a short guide meant to get you up and running quickly.

Creating a new menu

When adding a new menu you need to specify the menu name. This is the name that will be used to access the menu object in the future.

$menu = \Menu::getMenu('leftMenu');
Adding a drop down to the menu

To add a dop down just add a drop down method. The first parameter is the slug. The second is the text for the drop down. The third is a call back where you wil add your link methods.

$rightMenu->dropDown('user', \Auth::user()->username, function (DropDown $dropDown) {
    $dropDown->link('profile.edit', function (Link $link) {
        $link->name = 'Edit your profile';
        $link->url  = 'user/profile/';
    });
    $dropDown->link('logout', function (Link $link) {
        $link->name = 'Logout';
        $link->url  = route('logout');
    });
});
Accessing the menu from your view or controller

In your view or controller just you will need to call render

$menu = \Menu::render('leftMenu');

This will return your menu object. From there you can loop through the links and add the appropriate html.

@foreach ($menu->link as $link)
    <a href='{{$link->url}}'>{{$link->name}}</a>
@endforeach

Middleware

Middleware is provided with the menu package. This middle ware allows you to set which links are marked as active. This is done via the laravel routes file.

This middleware is compatible with Laravel 5.0 and 5.1.

Laravel 5.0

In laravel 5.0 you need to specify what middle ware you will use and the active link.

Route::get('/', [
    'middleware' => 'menu',
    'active' => 'home', // The slug of your menu item.
    'as'   => 'home',
    'uses' => 'HomeController@index'
]);
Laravel 5.1

In laravel 5.1 you can set the middle ware and the active item all in one line.

Route::get('/', [
    'middleware' => 'menu:home', // menu is the middle ware and home is the slug of your menu item.
    'as'   => 'home',
    'uses' => 'HomeController@index'
]);
Notes

This middle ware can also be used in the Route::group method as well.

Route::group(['namespace' => 'App\Http\Controllers', 'middleware' => 'menu:home'], function () {
    Route::get('/', [
        'as'   => 'home',
        'uses' => 'HomeController@index'
    ]);
});

Examples

Full code sample

Here is a full menu example.

$leftMenu = \Menu::getMenu('leftMenu');
$rightMenu = \Menu::getMenu('rightMenu');

// Home
$leftMenu->link('home', function (Link $link) {
    $link->name = 'Home';
    $link->url = route('home');
});

if (\Auth::guest()) {
    $rightMenu->link('login', function (Link $link) {
        $link->name = 'Login';
        $link->url = route('login');
    });

    $rightMenu->link('register', function (Link $link) {
        $link->name = 'Register';
        $link->url = route('register');
    });
} else {
    \Auth::user()->updateLastActive();

    if (\Auth::user()->is('ADMIN')) {
        $rightMenu->dropDown('admin', 'Admin', function (DropDown $dropDown) {
            $dropDown->link('dashboard', function (Link $link) {
                $link->name = 'Dashboard';
                $link->url  = route('admin.index');
            });
        });
    }

    $rightMenu->dropDown('user', \Auth::user()->username, function (DropDown $dropDown) {
        $dropDown->link('profile.edit', function (Link $link) {
            $link->name = 'Edit your profile';
            $link->url  = 'user/profile/';
        });
        $dropDown->link('logout', function (Link $link) {
            $link->name = 'Logout';
            $link->url  = route('logout');
        });
    });

    $rightMenu->link('profile.view', function (Link $link) {
        $link->name = 'Public profile';
        $link->url  = 'user/view/' . \Auth::user()->id;
        $link->insertAfter('profile.edit');
    });
}
Output
NukaCode\Menu\Menu Object
(
    [name] => right_menu
    [links] => Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => NukaCode\Menu\Link Object
                        (
                            [menu] => NukaCode\Menu\Menu Object
 *RECURSION*
                            [slug] => login
                            [name] => Login
                            [url] => http://localhost:5565/login
                            [options] => Array
                                (
                                )

                            [insert] =>
                            [active] =>
                        )

                    [1] => NukaCode\Menu\Link Object
                        (
                            [menu] => NukaCode\Menu\Menu Object
 *RECURSION*
                            [slug] => register
                            [name] => Register
                            [url] => http://localhost:5565/register
                            [options] => Array
                                (
                                )

                            [insert] =>
                            [active] =>
                        )

                )

        )

    [insert] =>
)
NukaCode\Menu\Menu Object
(
    [name] => left_menu
    [links] => Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => NukaCode\Menu\Link Object
                        (
                            [menu] => NukaCode\Menu\Menu Object
 *RECURSION*
                            [slug] => home
                            [name] => Home
                            [url] => http://localhost:5565
                            [options] => Array
                                (
                                )

                            [insert] =>
                            [active] =>
                        )

                )

        )

    [insert] =>
)