Skip to content

API Cheat Sheet

The public methods of the core classes. See Helper & Render Options for the Menu helper.

Factories, building, querying, and lifecycle. (MenuInterface)

php
Menu::create(array $attributes = []): static
Menu::fromArray(array $config): static          // build from a nested config array
Menu::fromFlat(iterable $rows, Closure $mapper): static  // build a tree from flat rows
MethodPurpose
add(ItemInterface $item)Add an existing item.
addItems(array $items)Add multiple existing items in order.
addItem(string $label, $link = null, array $options = [])Create and add an item.
addRaw(string $html, array $options = [])Add a raw-HTML item.
addDivider(array $options = [])Add a divider.
addHeader(string $label, array $options = [])Add a non-link section header.
newItem(?string $label = null, $link = null, array $options = [])Build an item without adding it.
getItems() / setItems(array)Root items as an array.
collect()All items (root + descendants) as an ItemCollection.
get(string $id) / has(string $id) / remove(string $id)Find/check/remove by id (recursive).
getByKey(string $key) / hasKey(string $key) / removeByKey(string $key)Find/check/remove by key — explicit or label slug; targets the first match (recursive).
insertBefore(ItemInterface $item, string $idOrKey) / insertAfter(...)Insert relative to a sibling (by id/key).
moveToPosition(string $idOrKey, int $pos) / moveToFirstPosition(...) / moveToLastPosition(...)Reposition a child.
reorder(list<string> $order)Reorder children by id/key; unlisted keep order, appended.
merge(MenuInterface $menu, bool $mergeAttributes = false)Append a deep copy of another menu's items (source untouched).
slice(int|string $offset, int|string|null $length = null)New menu with a copy of a range of items.
split(int|string $length)['primary' => ..., 'secondary' => ...] menus split at a boundary.
getActiveItem()Deepest active item.
clearActive()Deactivate all items.
getAttributes() / setAttribute() / setAttributes()Root HTML attributes.
getData() / setData()Menu-level metadata.
filter(callable)Keep only matching items.
find(callable)Return matching items as an ItemCollection without mutating the tree.
sortBy(callable|string $by, string $direction = Menu::SORT_ASC)Sort items.
resolve(ResolverInterface|ResolverCollectionInterface)Apply a resolver.
resetState()Reset active/visible/expanded to defaults.
freeze() / isFrozen()Make immutable / check.
toArray()Serialize (round-trips with fromArray()).

Item

A single entry. (ItemInterface)

GroupMethods
IdentitysetId() / getId(), setKey() / getKey(), hasExplicitKey()
LabelsetLabel(string, bool $escape = true) / getLabel(), shouldEscapeLabel()
LinksetLink() / getLink()
ContentsetRaw() / getRaw() / isRaw(), setBefore() / getBefore(), setAfter() / getAfter()
TypesetDivider() / isDivider(), setHeader() / isHeader()
Icon/badgesetIcon() / getIcon(), setBadge($badge, $type) / getBadge() / getBadgeType()
StatesetActive() / isActive(), setVisibility() / isVisible(), setExpanded() / isExpanded()
Submenuadd(), setSubMenu() / getSubMenu() / hasSubMenu(), setDisplayChildren(bool) / displaysChildren()
Label attrssetLabelAttributes(array, bool $merge = false) / getLabelAttributes()
TreesetParent() / getParent() / hasParent() / getParentId()
MatchingsetMatchRoutes() / addMatchRoute() / getMatchRoutes(), setIgnoreQueryString() / getIgnoreQueryString(), setFuzzyMatch() / isFuzzyMatch()
Attributes/datasetAttribute() / setAttributes() / getAttributes(), setData() / getData()
Lifecyclefreeze() / isFrozen(), resetState(), toArray()
Runtime (resolver-safe)setRuntimeActive(), setRuntimeVisibility(), setRuntimeExpanded()

The URL of an item. (LinkInterface)

php
Link::create(string|array|null $url = null, array $attributes = [], bool $external = false): static
MethodPurpose
setUrl($url, bool $external = false)Set the URL (string/array) and external flag.
getRawUrl()The raw URL (string, array, or null).
getUrl()The resolved URL string (via the Router).
isExternal()Whether the link is external.
setAttribute() / setAttributes() / getAttributes()Link HTML attributes.

ItemCollection

A flat collection returned by Menu::collect(). Iterable and countable.

MethodPurpose
add() / addMany()Add item(s).
all()Items as an array.
findById(string $id)Find by id.
findByKey(string $key)Find by key.
findByParent(string|ItemInterface $parent)Direct children of a parent.
count()Number of items.

Released under the MIT License.