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 labelLookup key (auto-slugified from the label if unset).
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 or markup.
badgestring|int|nullnullBadge text.
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).
fuzzyboolfalseEnable fuzzy (prefix) route matching for this item.
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, raw, and the icon/badge markup are emitted as-is — they are not escaped. Cast or escape any dynamic value you put there 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'])
    ->setFuzzyMatch()
    ->setExpanded();
OptionSetter
idsetId()
keysetKey()
label / escapesetLabel($label, $escape)
link / externalsetLink() (external via Link::create($url, [], true))
before / aftersetBefore() / setAfter()
iconsetIcon()
badge / badgeTypesetBadge($badge, $type)
attributessetAttribute() / setAttributes()
datasetData($name, $value)
visiblesetVisibility()
activesetActive()
rawsetRaw()
dividersetDivider()
headersetHeader()
matchRoutessetMatchRoutes() / addMatchRoute()
ignoreQueryStringsetIgnoreQueryString()
fuzzysetFuzzyMatch()
expandedsetExpanded()
displayChildrensetDisplayChildren()
labelAttributessetLabelAttributes()

See the full method list in the API Cheat Sheet.

Released under the MIT License.