PHP code example of qoraiche / laravel-menu-enhanced
1. Go to this page and download the library: Download qoraiche/laravel-menu-enhanced library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
Menu::makeOnce('primary', function(){}); // Creates primary, and executes callback.
Menu::makeOnce('primary', function(){}); // No operation.
{!! $MyNavBar->asUl() !!}
{!! Menu::get('MyNavBar')->asUl() !!}
$menu->add('About Us', 'about-us');
// Suppose we have these routes defined in our app/routes.php file
//...
Route::get('/', ['as' => 'home.page', function(){...}]);
Route::get('about', ['as' => 'page.about', function(){...}]);
//...
// Now we make the menu:
Menu::make('MyNavBar', function($menu){
$menu->add('Home', ['route' => 'home.page']);
$menu->add('About', ['route' => 'page.about']);
});
$menu->add('Members', 'members')->link->secure();
// or alternatively use the following method
$menu->add('Members', ['url' => 'members', 'secure' => true]);
Menu::make('MyNavBar', function($menu){
//...
$menu->add('About', ['route' => 'page.about']);
// these items will go under Item 'About'
// refer to about as a property of $menu object then call `add()` on it
$menu->about->add('Who We are', 'who-we-are');
// or
$menu->get('about')->add('What We Do', 'what-we-do');
// or
$menu->item('about')->add('Our Goals', 'our-goals');
//...
});
$menu->add('About', ['route' => 'page.about'])
->nickname('about_menu_nickname');
// And use it like you normally would
$menu->item('about_menu_nickname');
$menu->add('About', ['route' => 'page.about', 'nickname' => 'about_menu_nickname']);
// And use it like you normally would
$menu->item('about_menu_nickname');
$menu->itemTitleInCamelCase
// or
$menu->get('itemTitleInCamelCase')
// or
$menu->item('itemTitleInCamelCase')
$menu->add('About us', 'about-us')
$menu->aboutUs->divide();
// or
$menu->get('aboutUs')->divide();
// or
$menu->item('aboutUs')->divide();
$about = $menu->add('About', 'about');
$about->add('Who We Are', 'who-we-are');
$about->add('What We Do', 'what-we-do');
$menu->all();
// or outside of the builder context
Menu::get('MyNavBar')->all();
$menu->first();
// or outside of the builder context
Menu::get('MyNavBar')->first();
$menu->last();
// or outside of the builder context
Menu::get('MyNavBar')->last();
$menu->active()
// or outside of the builder content
Menu::get('MyNavBar')->active();
$aboutSubs = $menu->about->children();
// or outside of the builder context
$aboutSubs = Menu::get('MyNavBar')->about->children();
// or
$aboutSubs = Menu::get('MyNavBar')->item('about')->children();
if( $menu->about->hasChildren() ) {
// Do something
}
// or outside of the builder context
Menu::get('MyNavBar')->about->hasChildren();
// Or
Menu::get('MyNavBar')->item('about')->hasChildren();
$aboutSubs = $menu->about->all();
$aboutParent = $menu->about->parent();
// or outside of the builder context
$aboutParent = Menu::get('MyNavBar')->about->parent();
// Or
$aboutParent = Menu::get('MyNavBar')->item('about')->parent();
if( $menu->about->hasParent() ) {
// Do something
}
// or outside of the builder context
Menu::get('MyNavBar')->about->hasParent();
// Or
Menu::get('MyNavBar')->item('about')->hasParent();
$subs = $menu->whereParent(12);
$menu->add('Home', '#')->data('color', 'red');
$menu->add('About', '#')->data('color', 'blue');
$menu->add('Services', '#')->data('color', 'red');
$menu->add('Contact', '#')->data('color', 'green');
// Fetch all the items with color set to red:
$reds = $menu->whereColor('red');
$reds = $menu->whereColor('red', true);
$menu = Menu::get('MyNavBar');
$menus = Menu::all();
$menus = Menu::getCollection();
Menu::make('MyNavBar', function($menu){
// As you see, you need to pass the second parameter as an associative array:
$menu->add('Home', ['route' => 'home.page', 'class' => 'navbar navbar-home', 'id' => 'home']);
$menu->add('About', ['route' => 'page.about', 'class' => 'navbar navbar-about dropdown']);
$menu->add('services', ['action' => 'ServicesController@index']);
$menu->add('Contact', 'contact');
});
$menu->add('About', 'about');
$menu->about->add('Who we are', 'about/whoweare');
$menu->about->add('What we do', 'about/whatwedo');
// add a class to children of About
$menu->about->children()->attr('class', 'about-item');
//...
$menu->add('Separated Item', 'item-url')->divide()
// You can also use it this way:
$menu->('Another Separated Item', 'another-item-url');
// This line will insert a divider after the last defined item
$menu->divide()
//...
/*
* Output as <ul>:
*
* <ul>
* ...
* <li><a href="item-url">Separated Item</a></li>
* <li class="divider"></li>
*
* <li><a href="another-item-url">Another Separated Item</a></li>
* <li class="divider"></li>
* ...
* </ul>
*
*/
//...
$menu->add('Users', '#')->data('placement', 12);
// you can refer to placement as if it's a public property of the item object
echo $menu->users->placement; // Output : 12
//...
$menu->add('Users', 'users');
$menu->users->add('New User', 'users/new');
$menu->users->add('Uses', 'users');
// add a meta data to children of Users
$menu->users->children()->data('anything', 'value');
<ul class="control-items-min">
<li title="Menu">
<a data-toggle="dropdown" aria-expanded="true"><i class="fa fa-bars"></i> <span></span></a>
<!-- The first array is the attributes for the list: for example, `ul`;
The second is the attributes for the child lists, for example, `ul>li>ul`;
The third array is attributes that are added to the attributes of the `li` element. -->
echo $controlItem->asUl(['class'=>'dropdown-menu', 'role'=>'menu'],[],['class'=>'dropdown-item']);