PHP Class Spatie\Menu\Menu

Inheritance: implements Spatie\Menu\Item, implements Countabl\Countable, implements Spatie\Menu\HasHtmlAttributes, implements Spatie\Menu\HasParentAttributes, use trait Spatie\Menu\Traits\HasHtmlAttributes, use trait Spatie\Menu\Traits\HasParentAttributes
Datei anzeigen Open project: spatie/menu

Protected Properties

Property Type Description
$activeClass string
$append
$filters array
$htmlAttributes
$items array
$parentAttributes
$prepend
$wrap array

Public Methods

Method Description
__toString ( ) : string
add ( Spatie\Menu\Item $item ) Add an item to the menu. This also applies all registered filters to the item.
addIf ( boolean $condition, Spatie\Menu\Item $item ) Add an item to the menu if a (non-strict) condition is met.
addItemClass ( string $class ) Add a class to all items in the menu.
addItemParentClass ( string $class ) Add a parent class to all items in the menu.
append ( string $append ) Append a string of html to the menu on render.
appendIf ( boolean $condition, string $append ) : static Append the menu with a string of html on render if a certain condition is met.
applyToAll ( callable $callable ) Apply a callable to all existing items, and register it as a filter so it will get applied to all new items too. If you typehint the item parameter in the callable, it wil only be applied to items of that type.
blueprint ( ) : static Create a empty blueprint of the menu (copies filters and activeClass).
build ( array | Iterator $items, callable $callback, Menu $initial = null ) : static Build a new menu from an array. The callback receives a menu instance as the accumulator, the array item as the second parameter, and the item's key as the third.
count ( ) : integer The amount of items in the menu.
each ( callable $callable ) Iterate over all the items and apply a callback. If you typehint the item parameter in the callable, it wil only be applied to items of that type.
fill ( array | Iterator $items, callable $callback ) : static Fill a menu from an array. The callback receives a menu instance as the accumulator, the array item as the second parameter, and the item's key as the third.
html ( string $html, array $parentAttributes = [] ) Shortcut function to add raw html to the menu.
htmlIf ( boolean $condition, string $html, array $parentAttributes = [] ) Add a chunk of html if a (non-strict) condition is met.
isActive ( ) : boolean Determine whether the menu is active.
link ( string $url, string $text ) Shortcut function to add a plain link to the menu.
linkIf ( boolean $condition, string $url, string $text ) Add a link to the menu if a (non-strict) condition is met.
new ( array $items = [] ) : static Create a new menu, optionally prefilled with items.
prepend ( string $prepend ) Prepend the menu with a string of html on render.
prependIf ( boolean $condition, string $prepend ) Prepend the menu with a string of html on render if a certain condition is met.
registerFilter ( callable $callable ) Register a filter to the menu. When an item is added, all filters will be applied to the item. If you typehint the item parameter in the callable, it will only be applied to items of that type.
render ( ) : string Render the menu.
setActive ( callable | string $urlOrCallable, string $root = '/' ) Set multiple items in the menu as active based on a callable that filters through items. If you typehint the item parameter in the callable, it will only be applied to items of that type.
setActiveClass ( string $class ) Set the class name that will be used on active items for this menu.
setActiveFromCallable ( callable $callable )
setActiveFromUrl ( string $url, string $root = '/' ) Set all relevant children active based on the current request's URL.
setItemAttribute ( string $attribute, string $value = '' ) Set an attribute on all items in the menu.
setItemParentAttribute ( string $attribute, string $value = '' ) Add a parent attribute to all items in the menu.
submenu ( callable | Menu | Spatie\Menu\Item $header, callable | Menu | null $menu = null )
submenuIf ( boolean $condition, callable | Menu | Spatie\Menu\Item $header, callable | Menu | null $menu = null )
wrap ( string $element, array $attributes = [] ) Wrap the menu in an html element.

Protected Methods

Method Description
__construct ( variadic $items )
applyFilter ( callable $filter, Spatie\Menu\Item $item ) Apply a filter to an item. Returns the result of the filter.
createSubmenuHeader ( Spatie\Menu\Item | string $header ) : string
createSubmenuMenu ( Menu | callable $menu ) : Menu
parseSubmenuArgs ( $args ) : array

Method Details

__construct() protected method

protected __construct ( variadic $items )
$items variadic

__toString() public method

public __toString ( ) : string
return string

add() public method

Add an item to the menu. This also applies all registered filters to the item.
public add ( Spatie\Menu\Item $item )
$item Spatie\Menu\Item

addIf() public method

Add an item to the menu if a (non-strict) condition is met.
public addIf ( boolean $condition, Spatie\Menu\Item $item )
$condition boolean
$item Spatie\Menu\Item

addItemClass() public method

Add a class to all items in the menu.
public addItemClass ( string $class )
$class string

addItemParentClass() public method

Add a parent class to all items in the menu.
public addItemParentClass ( string $class )
$class string

append() public method

Append a string of html to the menu on render.
public append ( string $append )
$append string

appendIf() public method

Append the menu with a string of html on render if a certain condition is met.
public appendIf ( boolean $condition, string $append ) : static
$condition boolean
$append string
return static

applyFilter() protected method

Apply a filter to an item. Returns the result of the filter.
protected applyFilter ( callable $filter, Spatie\Menu\Item $item )
$filter callable
$item Spatie\Menu\Item

applyToAll() public method

Apply a callable to all existing items, and register it as a filter so it will get applied to all new items too. If you typehint the item parameter in the callable, it wil only be applied to items of that type.
public applyToAll ( callable $callable )
$callable callable

blueprint() public method

Create a empty blueprint of the menu (copies filters and activeClass).
public blueprint ( ) : static
return static

build() public static method

Build a new menu from an array. The callback receives a menu instance as the accumulator, the array item as the second parameter, and the item's key as the third.
public static build ( array | Iterator $items, callable $callback, Menu $initial = null ) : static
$items array | Iterator
$callback callable
$initial Menu
return static

count() public method

The amount of items in the menu.
public count ( ) : integer
return integer

createSubmenuHeader() protected method

protected createSubmenuHeader ( Spatie\Menu\Item | string $header ) : string
$header Spatie\Menu\Item | string
return string

createSubmenuMenu() protected method

protected createSubmenuMenu ( Menu | callable $menu ) : Menu
$menu Menu | callable
return Menu

each() public method

Iterate over all the items and apply a callback. If you typehint the item parameter in the callable, it wil only be applied to items of that type.
public each ( callable $callable )
$callable callable

fill() public method

Fill a menu from an array. The callback receives a menu instance as the accumulator, the array item as the second parameter, and the item's key as the third.
public fill ( array | Iterator $items, callable $callback ) : static
$items array | Iterator
$callback callable
return static

html() public method

Shortcut function to add raw html to the menu.
public html ( string $html, array $parentAttributes = [] )
$html string
$parentAttributes array

htmlIf() public method

Add a chunk of html if a (non-strict) condition is met.
public htmlIf ( boolean $condition, string $html, array $parentAttributes = [] )
$condition boolean
$html string
$parentAttributes array

isActive() public method

Determine whether the menu is active.
public isActive ( ) : boolean
return boolean

linkIf() public method

Add a link to the menu if a (non-strict) condition is met.
public linkIf ( boolean $condition, string $url, string $text )
$condition boolean
$url string
$text string

new() public static method

Create a new menu, optionally prefilled with items.
public static new ( array $items = [] ) : static
$items array
return static

parseSubmenuArgs() protected method

protected parseSubmenuArgs ( $args ) : array
return array

prepend() public method

Prepend the menu with a string of html on render.
public prepend ( string $prepend )
$prepend string

prependIf() public method

Prepend the menu with a string of html on render if a certain condition is met.
public prependIf ( boolean $condition, string $prepend )
$condition boolean
$prepend string

registerFilter() public method

Register a filter to the menu. When an item is added, all filters will be applied to the item. If you typehint the item parameter in the callable, it will only be applied to items of that type.
public registerFilter ( callable $callable )
$callable callable

render() public method

Render the menu.
public render ( ) : string
return string

setActive() public method

Set multiple items in the menu as active based on a callable that filters through items. If you typehint the item parameter in the callable, it will only be applied to items of that type.
public setActive ( callable | string $urlOrCallable, string $root = '/' )
$urlOrCallable callable | string
$root string

setActiveClass() public method

Set the class name that will be used on active items for this menu.
public setActiveClass ( string $class )
$class string

setActiveFromCallable() public method

public setActiveFromCallable ( callable $callable )
$callable callable

setActiveFromUrl() public method

, /about, /contact => request to /about will set the about link active. en, /en/about, /en/contact => request to /en won't set /en active if the request root is set to /en.
public setActiveFromUrl ( string $url, string $root = '/' )
$url string The current request url.
$root string If the link's URL is an exact match with the request root, the link won't be set active. This behavior is to avoid having home links active on every request.

setItemAttribute() public method

Set an attribute on all items in the menu.
public setItemAttribute ( string $attribute, string $value = '' )
$attribute string
$value string

setItemParentAttribute() public method

Add a parent attribute to all items in the menu.
public setItemParentAttribute ( string $attribute, string $value = '' )
$attribute string
$value string

submenu() public method

public submenu ( callable | Menu | Spatie\Menu\Item $header, callable | Menu | null $menu = null )
$header callable | Menu | Spatie\Menu\Item
$menu callable | Menu | null

submenuIf() public method

public submenuIf ( boolean $condition, callable | Menu | Spatie\Menu\Item $header, callable | Menu | null $menu = null )
$condition boolean
$header callable | Menu | Spatie\Menu\Item
$menu callable | Menu | null

wrap() public method

Wrap the menu in an html element.
public wrap ( string $element, array $attributes = [] )
$element string
$attributes array

Property Details

$activeClass protected_oe property

protected string $activeClass
return string

$append protected_oe property

protected $append

$filters protected_oe property

protected array $filters
return array

$htmlAttributes protected_oe property

protected $htmlAttributes

$items protected_oe property

protected array $items
return array

$parentAttributes protected_oe property

protected $parentAttributes

$prepend protected_oe property

protected $prepend

$wrap protected_oe property

protected array $wrap
return array