Skip to content

Item Options ​

The third argument to addItem() / newItem() (and each entry's keys in Menu::fromArray()) is an options array. Every key is optional.

php
$menu->addItem('Inbox', ['controller' => 'Messages', 'action' => 'index'], [
    'icon' => 'fa fa-inbox',
    'badge' => $unread,
    'badgeType' => 'bg-danger',
    'data' => ['section' => ['controller' => 'Messages']],
]);

Options ​

KeyTypeDefaultMeaning
idstringauto UUIDUnique item identifier.
keystringslug of labelExplicit keys take precedence in lookup; an unset key falls back to the label slug.
labelstring(from arg)Item text.
escapebooltrueHTML-escape the label.
linkstring|array|LinkInterface|nullnullURL; string, CakePHP route array, or LinkInterface.
linkAttributesarray[]HTML attributes on the <a>.
externalboolfalseMark the link external (skips routing).
beforestring''Trusted markup before the label.
afterstring''Trusted markup after the label.
iconstring|nullnullIcon class, escaped before insertion into iconTemplate.
escapeIconbooltrueSet false to insert trusted icon markup as-is, e.g. '<i class="bi bi-house"></i>'. Same as setIcon($icon, false).
badgestring|int|nullnullBadge text, HTML-escaped by default.
escapeBadgebooltrueSet false to insert trusted badge markup as-is. Same as setBadge($badge, $type, false).
badgeTypestring|nullnullBadge CSS class/type.
attributesarray[]HTML attributes on the <li>.
dataarray[]Arbitrary metadata (read by resolvers, e.g. section, permission, roles).
visiblebooltrueInitial visibility.
activeboolfalseInitial active state.
rawstring|nullnullCustom HTML content (bypasses the label/link).
dividerboolfalseRender as a divider only.
headerboolfalseRender as a non-link section header.
submenuAttributesarray[]HTML attributes on the submenu <ul>.
matchRoutesarray[]Extra route patterns that mark the item active.
ignoreQueryStringbool|nullnullIgnore the query string when matching (overrides the resolver default).
fuzzybool|nullnullFuzzy (prefix) route matching for this item. null inherits the resolver's setting, which defaults to true; true or false overrides it.
expandedboolfalseShow the submenu expanded by default.
displayChildrenbooltrueWhen false, render the item but not its submenu (treated as a leaf).
labelAttributesarray[]HTML attributes on the rendered link/label element (classes merge).

Trusted markup

before, after, and raw are emitted as-is. Icon and badge values are escaped by default; their Item setters can opt out for trusted markup. Cast or escape dynamic values in raw fields yourself (e.g. (int)$count). The label is escaped unless escape is false.

Fluent equivalents ​

Anything you can pass as an option has a setter on ItemInterface, so you can build incrementally:

php
$item = $menu->addItem('Profile', '/profile')
    ->setIcon('fa fa-user')
    ->setBadge('new', 'bg-success')
    ->setData('roles', ['admin'])
    ->setFuzzy(true)
    ->setExpanded();
OptionSetter
idsetId()
keysetKey()
label / escapesetLabel($label, $escape)
link / externalsetLink() (external via Link::create($url, [], true))
before / aftersetBefore() / setAfter()
iconsetIcon($icon, $escape)
badge / badgeTypesetBadge($badge, $type, $escape)
attributessetAttribute() / setAttributes()
datasetData($name, $value)
visiblesetVisible()
activesetActive()
rawsetRaw()
dividersetDivider()
headersetHeader()
matchRoutessetMatchRoutes() / addMatchRoute()
ignoreQueryStringsetIgnoreQueryString()
fuzzysetFuzzy(true)
expandedsetExpanded()
displayChildrensetDisplayChildren()
labelAttributessetLabelAttributes()

See the full method list in the API Cheat Sheet.

setVisible(), setActive(), and setExpanded() change authoring defaults. Runtime setters override those defaults until resetState(). toArray() serializes defaults.

setFuzzy(?bool) and getFuzzy(): ?bool use null to inherit the resolver setting, true for fuzzy matching, and false for exact matching. The option key remains fuzzy.

Released under the MIT License.