Wallogit.com
2017 © Pedro Peláez
I just got tired of writing foreach in foreach in foreach :) So I build simple package for Laravel 4.* to create any depth menu lists breadcrumbs etc., (*1)
Require this package in your composer.json and run composer update:, (*2)
"luknei/navigator": "dev-master"
After updating composer, add the ServiceProvider to the providers array in app/config/app.php, (*3)
'Luknei\Navigator\NavigatorServiceProvider',
Add this to your facades in app.php:, (*4)
'Nav' => 'Luknei\Navigator\NavigatorFacade',
//First level/depth items:
//menu group - "menu", menu item - "auth", and variables of the template
Nav::group('menu')->add('auth',[
'title' => trans('auth::base.auth'),
'href' => '#',
'icon' => 'icon-key',
]);
//Second level/depth items:
//menu group - "menu", menu item - "auth.groups", and variables of the template
Nav::group('menu')->add('auth.groups', [
'title' => trans('auth::base.groups'),
'href' => URL::action('Auth\GroupsController@getAll'),
]);
Template for the first level/depth items Templates have few elements: - @depth() ... @stop - @foreach ... @endforeach - @subgroup - variables you passed when adding element to the group $title, $icon etc, (*5)
These parameters must be in every template, even though you don't need @subgroup you should use it anyway @subgroup should be placed there where you would have nested @foreach() ... @endforeach, (*6)
@depth(1)
<ul class="nav nav-list">
@foreach
<li>
<a href="{{ $href }}" class="dropdown-toggle">
<i class="{{ $icon }}"></i>
<span class="menu-text"> {{ $title }} </span>
</a>
@subgroup
</li>
@endforeach
</ul>
@stop
You can Set the default template for any level/depth item, (*7)
@depth(default)
<ul class="submenu" style="display: none;">
@foreach
<li>
<a href="{{ $href }}">
<i class="icon-double-angle-right"></i>
{{ $title }}
</a>
@subgroup
</li>
@endforeach
</ul>
@stop
There are also options for even and odd depth/levels @depth(even) @depth(odd), (*8)
To render your navigation menu simply set the group and use render method The first parameter of render method is your template, usage is the same as you use View::make();, (*9)
Nav::group('admin.menu')->render('partials.sidemenu');
If you want to cache your rendered menu for more performance, pass a second parameter - minutes to keep the cache., (*10)
Nav::group('admin.menu')->render('partials.sidemenu', 20); //cached for 20min