PHP code example of llama-laravel / menus

1. Go to this page and download the library: Download llama-laravel/menus 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/ */

    

llama-laravel / menus example snippets




'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
	
	...
        
        Llama\Menus\MenuServiceProvider::class,
        
        ...

],



'aliases' => [

    'App'       => Illuminate\Support\Facades\App::class,
    'Artisan'   => Illuminate\Support\Facades\Artisan::class,
    ...
    'Menu'       => Llama\Menus\Facades\Menu::class,

],


\Menu::macro('MyNavBar', function($menu){
  
  $menu->push('Home');
  $menu->push('About',    'about');
  $menu->push('services', 'services');
  $menu->push('Contact',  'contact');
  
});

{!! $MyNavBar->asUl() !!}

{!! \Menu::get('MyNavBar')->asUl() !!}


// ...
$menu->push('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::macro('MyNavBar', function($menu){
  
  $menu->push('Home', route('home.page'));
  $menu->push('About', route('page.about'));
  // ...

});



// Suppose we have these routes defined in our app/Http/routes.php file

// ...
Route::get('services', 'ServiceController@index');
//...


  // ...
  $menu->push('services', action('ServicesController@index'));
  // ...



\Menu::macro('MyNavBar', function($menu){
  
  $menu->push('Home', route('home.page'));
  $menu->push('About', route('page.about', ['template' => 1]));
  $menu->push('services', action('ServicesController@index', ['id' => 12]));
 
  $menu->push('Contact',  'contact');

});


	// ...
	$menu->push('Members', 'members')->link->secure();
	
	
	// or alternatively use the following method
	
	$menu->push('Members', ['url' => 'members', 'secure' => true]);
	
	// ...



\Menu::macro('MyNavBar', function($menu){

  //...
  
  $menu->push('About', route('page.about'));
  
  // these items will go under Item 'About'
  
  // refer to about as a property of $menu object then call `push()` on it
  $menu->about->push('Who We are', 'who-we-are');

  // or
  
  $menu->get('about')->push('What We Do', 'what-we-do');
  
  // or
  
  $menu->item('about')->push('Our Goals', 'our-goals');
  
  //...

});
  


  // ...
  
  $menu->push('About', route('page.about'))
		     ->push('Level2', 'link address')
		          ->push('level3', 'Link address')
		               ->push('level4', 'Link address');
        
  // ...      
  

	//...
	$menu->push('About', route('page.about'));
	$menu->push('Level2', ['url' => 'Link address', 'parent' => $menu->about->id]);
	//...


	// ...
	$menu->push('About', route('page.about'))
	     ->id('74398247329487')
	// ...


	// ...
	$menu->push('About', route('page.about', ['id' => 74398247329487]));
	// ...


	// ...
	$menu->push('About', route('page.about'))
	     ->nickname('about_menu_nickname');
	     
	// And use it like you normally would
	$menu->item('about_menu_nickname');     
	     
	// ...


	// ...
	$menu->push('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->push('About us', 'about-us')
	
	$menu->aboutUs->divide();
	
	// or
	
	$menu->get('aboutUs')->divide();
	
	// or
	
	$menu->item('aboutUs')->divide();
	
	// ...


// ...
$about = $menu->push('About', 'about');
$about->push('Who We Are', 'who-we-are');
$about->push('What We Do', 'what-we-do');
// ...


	// ...
	$menu->find(12) ...
	// ...


	// ...
	$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();
	// ...


	// ...
	$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();
// ...



	// ...
	$subs = $menu->whereParent(12);
	// ...


	// ...
	$menu->push('Home',     '#')->data('color', 'red');
	$menu->push('About',    '#')->data('color', 'blue');
	$menu->push('Services', '#')->data('color', 'red');
	$menu->push('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();
	// ...


\Menu::macro('MyNavBar', function($menu){

  // As you see, you need to pass the second parameter as an associative array:
  $menu->push('Home', ['url'  => route('home.page'),  'class' => 'navbar navbar-home', 'id' => 'home']);
  $menu->push('About', ['url'  => route('page.about'), 'class' => 'navbar navbar-about dropdown']);
  $menu->push('services', action('ServicesController@index'));
  $menu->push('Contact', 'contact');

});


	//...
	$menu->push('About', ['url' => 'about', 'class' => 'about-item']);
	
	echo $menu->about->attr('class');  // output:  about-item
	
	$menu->about->attr('class', 'another-class');
	echo $menu->about->attr('class');  // output:  about-item another-class

	$menu->about->attr(['class' => 'yet-another', 'id' => 'about']); 
	
	echo $menu->about->attr('class');  // output:  about-item another-class yet-another
	echo $menu->about->attr('id');  // output:  id
	
	print_r($menu->about->attr());
	
	/* Output
	Array
	(
		[class] => about-item another-class yet-another
		[id] => id
	)
	*/
	
	//...


  // ...
  $menu->push('About', 'about');
  
  $menu->about->push('Who we are', 'about/whoweare');
  $menu->about->push('What we do', 'about/whatwedo');
  
  // add a class to children of About
  $menu->about->children()->attr('class', 'about-item');
  
  // ...


\Menu::macro('MyNavBar', function($menu){

 //  ...
  
  $about = $menu->push('About', ['url'  => route('page.about'), 'class' => 'navbar navbar-about dropdown']);
  
  $about->link->attr(['class' => 'dropdown-toggle', 'data-toggle' => 'dropdown']);
  
 // ...
  
});


	// ...
	$menu->push('Home', '#')->active();
	// ...
	
	/* Output
	
	<li class="active"><a href="#">#</a></li>	
	
	*/
	


	// ...
	$menu->push('Home', '#')->link->active();
	// ...
	
	/* Output
	
	<li><a class="active" href="#">#</a></li>	
	
	*/
	


	// ...
	'activate.element' => 'item',    // item|link
	// ...



// ...
$menu->push('Articles', 'articles')->active('this-is-another-url/*');
// ...


	//...
	$menu->push('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->push('Separated Item', 'item-url')->divide(['class' => 'my-divider']);
	//...
	
	/*
	Output as <ul>:
	
		<ul>
			...
			<li><a href="item-url">Separated Item</a></li>
			<li class="my-divider divider"></li>
		
			...
		</ul>
		
	*/


\Menu::macro('MyNavBar', function($menu){

  // ...
  
  $about = $menu->push('About', ['url'  => route('page.about'), 'class' => 'navbar navbar-about dropdown']);
  
  $menu->about->attr(['class' => 'dropdown-toggle', 'data-toggle' => 'dropdown'])
              ->append(' <b class="caret"></b>')
              ->prepend('<span class="glyphicon glyphicon-user"></span> ');
              
  // ...            

});


    // ...
    $menu->raw('Item Title', ['class' => 'some-class']);  
    
    $menu->push('About', 'about');
    $menu->About->raw('Another Plain Text Item')
    // ...
    
    /* Output as an unordered list:
       <ul>
            ...
            <li class="some-class">Item's Title</li>
            <li>
                About
                <ul>
                    <li>Another Plain Text Item</li>
                </ul>
            </li>
            ...
        </ul>
    */


\Menu::macro('MyNavBar', function($menu){

  $menu->push('Home', ['url'  => route('home.page'), 'class' => 'navbar navbar-home', 'id' => 'home']);
  
  $menu->group(['style' => 'padding: 0', 'data-role' => 'navigation'], function($m){
        $m->push('About', ['url'  => route('page.about'), 'class' => 'navbar navbar-about dropdown']);
        $m->push('services', action('ServicesController@index'));
  }
  
  $menu->push('Contact',  'contact');

});


\Menu::macro('MyNavBar', function($menu){

  $menu->push('Home', ['url'  => route('home.page'), 'class' => 'navbar navbar-home', 'id' => 'home']);
  
  $menu->push('About', ['url'  => 'about', 'class' => 'navbar navbar-about dropdown']);  // URL: /about 
  
  $menu->group(['prefix' => 'about'], function($m){
  
  	$about->push('Who we are?', 'who-we-are');   // URL: about/who-we-are
  	$about->push('What we do?', 'what-we-do');   // URL: about/what-we-do
  	
  });
  
  $menu->push('Contact',  'contact');

});


\Menu::macro('MyNavBar', function($menu){

	// ...
	
	$menu->group(['prefix' => 'pages', 'data-info' => 'test'], function($m){
		
		$m->push('About', 'about');
		
		$m->group(['prefix' => 'about', 'data-role' => 'navigation'], function($a){
		
			$a->push('Who we are', 'who-we-are?');
			$a->push('What we do?', 'what-we-do');
			$a->push('Our Goals', 'our-goals');
		});
	});
	
});


\Menu::macro('MyNavBar', function($menu){

  // ...
  
  $menu->push('Users', route('admin.users'))
       ->data('permission', 'manage_users');

});


	
	//...
	
	$menu->push('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->push('Users', 'users');
  
  $menu->users->push('New User', 'users/new');
  $menu->users->push('Uses', 'users');
  
  // add a meta data to children of Users
  $menu->users->children()->data('anything', 'value');
  
  // ...


\Menu::macro('MyNavBar', function($menu){

  // ...
  
  $menu->push('Users', route('admin.users'))
       ->data('permission', 'manage_users');

})->filter(function($item){
  if(User::get()->can( $item->data('permission'))) {
      return true;
  }
  return false;
});


\Menu::macro('main', function($m){

	$m->push('About', '#')     ->data('order', 2);
	$m->push('Home', '#')      ->data('order', 1);
	$m->push('Services', '#')  ->data('order', 3);
	$m->push('Contact', '#')   ->data('order', 5);
	$m->push('Portfolio', '#') ->data('order', 4);

})->sortBy('order');		


\Menu::macro('main', function($m){

	$m->push('About');
	$m->push('Home');
	$m->push('Services');
	$m->push('Contact');
	$m->push('Portfolio');

})->sortBy('id', 'desc');		


\Menu::macro('main', function($m){

	$m->push('About')     ->data('order', 2);
	$m->push('Home')      ->data('order', 1);
	$m->push('Services')  ->data('order', 3);
	$m->push('Contact')   ->data('order', 5);
	$m->push('Portfolio') ->data('order', 4);

})->sortBy(function($items) {
	// Your sorting algorithm here...
	
});		

{!! $MenuName->asUl( ['class' => 'awesome-ul'] ) !!}

  {!! $MenuName->asOl() !!}

{!! $MenuName->asOl( ['class' => 'awesome-ol'] ) !!}

  {!! $MenuName->asDiv() !!}

{!! $MenuName->asDiv( ['class' => 'awesome-div'] ) !!}


\Menu::macro('MyNavBar', function($menu){
  
  $menu->push('Home');
  
   $menu->push('About', route('page.about'));
   
   $menu->about->push('Who are we?', 'who-we-are');
   $menu->about->push('What we do?', 'what-we-do');

  $menu->push('services', 'services');
  $menu->push('Contact',  'contact');
  
});

<nav class="navbar">
  <ul class="horizontal-navbar">
    @

@foreach($items as $item)
  <li @if($item->hasChildren()) class="dropdown" @endif>
      <a href="{!! $item->url() !!}">{!! $item->title !!} </a>
      @if($item->hasChildren())
        <ul class="dropdown-menu">
              @

...
@


return [
	'default' => [
		'activate'    => [
			'auto' => true,
			'parents' => true,
			'class' => 'active',
			'element'   => 'item', // item|link
		],
		'restful'          => true,
	],
	'your_menu_name' => [
		'activate'    => [
			'auto' => false
		]
	]
];


// ...
$menu->push('About')->link->href('#');
// ...
html
  {!! $MenuName->asUl() !!}