1. Go to this page and download the library: Download malezha/laravel-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/ */
malezha / laravel-menu example snippets
Malezha\Menu\MenuServiceProvider::class,
'Menu' => Malezha\Menu\MenuFacade::class,
use Malezha\Menu\Contracts\Builder;
use Menu;
Menu::make('main', function(Builder $builder) {
// Create menu element
$builder->create('element_name', ElementClass::class, function (ElementFactory $factory) {
$factory->elementParameter;
return $factory->build();
});
# Avaliable elements
// Link. Html:
// <li attributes|activeAttributes><a href="url" linkAttributes>title</a></li>
use Malezha\Menu\Element\Link;
use Malezha\Menu\Factory\LinkFactory;
$builder->create('link', Link::class, function (LinkFactory $factory) {
$factory->title = 'Title';
$factory->url = '/';
$factory->attributes->put('class', 'li');
$factory->activeAttributes->push(['class' => 'active-element']);
$factory->linkAttributes->set(['id' => 'link']);
$factory->displayRule = true; // Boolean or callable witch return boolean
return $factory->build();
});
// Submenu. Html:
// <li attributes|activeAttributes>
// <a href="url" linkAttributes>title</a>
// <ul>...</ul>
// </li>
use Malezha\Menu\Element\SubMenu;
use Malezha\Menu\Factory\SubMenuFactory;
$builder->create('submenu', SubMenu::class, function (SubMenuFactory $factory) {
// Submenu exdends Link so all parameters available
$factory->builder->create(...); // Create submenu element
return $factory->build();
});
// Text. Html:
// <li attributes>Text</li>
use Malezha\Menu\Element\Text;
use Malezha\Menu\Factory\TextFactory;
$builder->create('submenu', Text::class, function (TextFactory $factory) {
$factory->text = 'Text';
$factory->attributes->put('class', 'deliver');
$factory->displayRule = true;
return $factory->build();
});
});
// Building menu from array
$array = [
'type' => 'ul',
'view' => 'menu::view', // Default view
'attributes' => [
'class' => 'menu',
],
'activeAttributes' => [
'class' => 'active',
],
'elements' => [
'index' => [
'type' => 'link',
'view' => 'menu::elements.link', // Default view may be changed in config
'url' => 'http://example.com',
'attributes' => [],
'activeAttributes' => [
class' => 'active',
],
'linkAttributes' => [],
'displayRule' => true,
],
...
'settings' => [
'type' => 'submenu',
'view' => 'menu::elements.submenu',
'url' => 'http://example.com',
'attributes' => [],
'activeAttributes' => [
class' => 'active',
],
'linkAttributes' => [],
'displayRule' => true,
'builder' => [
'type' => 'ul',
'view' => '_partial.submenu', // U can set view for submenu singly
...
'elements' => [
...
],
],
],
]
];
$builder = Menu::fromArray('from-array', $array);
$html = $builder->render(); // Menu::render('from-array');
// $builder->toArray() === $array;