spatie/menu
Fluent, extensible menu builder for Laravel. Compose navigation with a clean API, render as HTML, and customize output via presenters and macros. Supports active state handling, links, submenus, and easy integration with Blade and your app’s routing.
There are three methods to manipulate items in a menu:
each: Goes over all existing items and applies a manipulationregisterFilter: Registers a manipulation that will be applied to all items added afterwardsapplyToAll: Applies a manipulation to all existing and all future itemsAll methods require a callable as their first and only parameter. The callable will receive the item as it's parameter. If this parameter is typehinted, the manipulation will only be applied to items of that type.
Menu::new()
->add(Link::to('/', 'Home'))
->add(Html::raw('<a href="#" data-avatar>Profile</a>'))
->each(function (Link $link) {
$link->addClass('link');
})
->each(function (Html $html) {
$html->addParentClass('html');
});
In the above example, all links will receive a link class, and all html chunk parents (li's) will receive an html class.
Iterates over all existing items, and applies a manipulation.
Menu::new()
->add(Link::to('/foo-before', 'Foo before'))
->add(Link::to('/bar-before', 'Bar before'))
->each(function (Link $link) {
// Return if string doesn't contain 'Foo'
if (strpos($link->getText(), 'Foo') === false) {
return;
}
$link->addClass('-has-foo');
})
->add(Link::to('/foo-after', 'Foo after'))
->add(Link::to('/bar-after', 'Bar after'))
<ul>
<li><a href="/foo-before" class="-has-foo">Foo before</a></li>
<li><a href="/bar-before">Bar before</a></li>
<li><a href="/foo-after">Foo after</a></li>
<li><a href="/bar-after">Bar after</a></li>
</ul>
Registers a manipulation that will be applied on every new item.
Menu::new()
->add(Link::to('/foo-before', 'Foo before'))
->add(Link::to('/bar-before', 'Bar before'))
->registerFilter(function (Link $link) {
// Return if string doesn't contain 'Foo'
if (strpos($link->getText(), 'Foo') === false) {
return;
}
$link->addClass('-has-foo');
})
->add(Link::to('/foo-after', 'Foo after'))
->add(Link::to('/bar-after', 'Bar after'))
<ul>
<li><a href="/foo-before">Foo before</a></li>
<li><a href="/bar-before">Bar before</a></li>
<li><a href="/foo-after" class="-has-foo">Foo after</a></li>
<li><a href="/bar-after">Bar after</a></li>
</ul>
Applies a manipulation to all existing and future items no matter where it's called.
Menu::new()
->add(Link::to('/foo-before', 'Foo before'))
->add(Link::to('/bar-before', 'Bar before'))
->applyToAll(function (Link $link) {
// Return if string doesn't contain 'Foo'
if (strpos($link->getText(), 'Foo') === false) {
return;
}
$link->addClass('-has-foo');
})
->add(Link::to('/foo-after', 'Foo after'))
->add(Link::to('/bar-after', 'Bar after'))
<ul>
<li><a href="/foo-before" class="-has-foo">Foo before</a></li>
<li><a href="/bar-before">Bar before</a></li>
<li><a href="/foo-after" class="-has-foo">Foo after</a></li>
<li><a href="/bar-after">Bar after</a></li>
</ul>
How can I help you explore Laravel packages today?