![Software License][ico-license]
, (*1)
Simple component for creating menus in Laravel. Instead of using controllers or creating huge blade templates suggests only one configuration file., (*2)
Example (resources/menu/default.php):, (*3)
<?php
return [
//'template' => 'menu.menu', // custom blade template can be provided here
'items' => [
[
'name' => 'Home',
'link' => route('dashboard'),
'active' => activeMenuController(\App\Http\Controllers\DashboardController::class),
'icon' => 'home',
],
[
'name' => 'Users',
'auth' => authMenuCan('manageUsers'), // Will be shown if user "can" 'manageUsers'.
// Gate::check('manageUsers') will be checked
'link' => '/users',
'active' => activeMenuUrlPrefix('users'), // Will be active for all '/users*' URL's
'icon' => 'user',
],
[
'name' => trans('complex.auth'),
'auth' => function (){
return Gate::check('one') || Gate::check('two');
},
'link' => '/complex-auth',
'active' => activeMenuUrlPrefix('complex-auth'),
'icon' => 'complex',
],
[
'name' => 'Group',
'icon' => 'group',
'items' => [
[
'name' => 'Orders',
'link' => '/orders', // route('orders'),
'active' => activeMenuController(\App\Http\Controllers\OrdersController::class),
'icon' => 'orders',
],
[
'name' => 'Bar',
'link' => '/bar', // route('bar'),
'active' => activeMenuController(\App\Http\Controllers\BarController::class),
'icon' => 'bar',
],
],
],
],
];
Blade template for one-level menus can be very simple:, (*4)
<!-- Example of menu template with Bootstrap and FontAwesome icons-->
<nav class="navbar navbar-default">
<ul class="nav navbar-nav">
@foreach($items as $item)
<li @if($item['active']) class="active" @endif>
<a href="{{$item['link']}}" title="{{$item['name']}}">
<i class="fa fa-{{$item['icon']}}"></i> {{$item['name']}}
</a>
</li>
@endforeach
</ul>
</nav>
For two-level menus it's more complicated. Example of it is provided by package., (*5)
Installation
First, require package via composer:, (*6)
composer require adel/laravel-menu
For Laravel < 5.5 check Laravel < 5.5 installation section., (*7)
Then publish some initial configuration and blade template:, (*8)
php artisan vendor:publish --provider="Adelf\LaravelMenu\LaravelMenuServiceProvider"
resources/menu/default.php and resources/views/menu/menu.blade.php files will appear. It's just an examples. Provide your own configuration and blade template., (*9)
Using
This will render menu's blade template, (*10)
<header>
{!! LaravelMenu::render() !!}
</header>
Title of active item can be used for title:, (*11)
<title>
{{array_get(LaravelMenu::getLastActiveItem(), 'name', 'Default title')}}
</title>
Another menu can be created by defining it in new file on resources/menu directory. For example, resources/menu/admin.php, (*12)
<title>
{!! array_get(LaravelMenu::getLastActiveItem('admin'), 'name', 'Default title') !!}
</title>
<header>
{!! LaravelMenu::render('admin') !!}
</header>
Laravel < 5.5 installation
Service provider and facade should be registered in config/app.php:, (*13)
'providers' => [
...
Adelf\LaravelMenu\LaravelMenuServiceProvider::class,
],
'aliases' => [
...,
'LaravelMenu' => Adelf\LaravelMenu\Facade::class,
],