PHP code example of loic-sharma / menu

1. Go to this page and download the library: Download loic-sharma/menu 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/ */

    

loic-sharma / menu example snippets




$factory = new Menu\Factory;

$menu = $factory->get('header');

$menu->add('Home', '/');
$menu->add('Blog', '/blog');

$menu->add('Menu', '/menu', function($menu)
{
	$menu->add('Item 1', '/menu/item-1');
	$menu->add('Item 2', '/menu/item-2');
});

echo $menu;




$menu->add('First Link', '/', function($item)
{
	$item->link->id = 'a-id';
	$item->link->class = 'a-class';

	$item->label->id = 'li-id';
	$item->label->class = 'li-class';
});

$menu->add('Second Link', function($item)
{
	$item->url = '/uri';

	$item->list->id = 'ul-id';
	$item->list->class = 'ul-class';

	$menu->add('Sub Link', '/sub/link');
});



// Filter all the menus.
$factory->addFilter(function($item)
{
	// Remove any items that have the name 'Admin'
	if($item->name == 'Admin')
	{
		$item->remove();
	}
});

// Filter a specific menu.
$menu->addFilter(function($item)
{
	// Modify the li elements.
	$item->label->class = 'prettify';
});