Skip to content

Helper & Render Options

The Menu helper builds, resolves, and renders named menus. Load it in AppView:

php
$this->loadHelper('Menu.Menu');

Methods

MethodPurpose
create(string $name, array $options = [])Create a new named menu.
register(string $name, callable $cb, array $options = [])Idempotently build a named menu via a callback.
getOrCreate(string $name, array $options = [])Return an existing menu or create it.
has(string $name)Whether a named menu exists.
get(string $name)Return a named menu (throws if missing).
remove(string $name) / reset()Drop one / all named menus.
render($menu = null, array $options = [])Resolve and render a menu (by name, instance, or the last-created).
getCurrentItem($menu = null, array $options = [])The deepest active item after resolution.
extractPath(ItemInterface $item)Root-to-item path (for breadcrumbs).
getBreadcrumbs($menu = null, array $options = [])Active path as an array of crumbs.
populateBreadcrumbs($menu = null, array $options = [])Push crumbs into Cake's Breadcrumbs helper.
renderBreadcrumbs($menu = null, $options = [], $attributes = [], $separator = [])Render breadcrumbs (via the BreadcrumbRenderer or Cake's helper).

render() options

These come from the helper's defaults and may be overridden per call. Unrecognized keys are passed straight through to the renderer as renderer options.

OptionDefaultDescription
rendererStringTemplateRenderer::classRenderer class name or instance.
resolvetrueSet false to render without resolving active state.
resolver(built-in URL resolvers)A ResolverInterface/ResolverCollectionInterface that replaces the defaults.
additionalResolvers[]Extra ResolverInterfaces appended after the defaults (keeps active-state matching).
singleActivefalseKeep only the deepest active item active (best-match arbitration).
fuzzytrueFuzzy (prefix) matching for the default UrlArrayResolver.
ignoreQueryStringtrueIgnore the query string in the default Psr7UrlResolver.
resolveDepthnullMax depth the default URL resolvers scan (null = unlimited).
currentAsLinktruePassed through: render the active item as a link.

Option precedence

Options merge in this order (later wins): helper configcreate()/register() optionsthis call's $options. So defaults set at create() time apply to every later render() unless overridden in the call.

resolver vs additionalResolvers

resolver replaces the built-in URL resolvers (you lose automatic active-state matching). To keep them and add your own, use additionalResolvers. See Resolvers.

create() / register() options

OptionDescription
attributes / menuAttributesHTML attributes for the root <ul>.
overwriteAllow create() to replace an existing menu of the same name.
rebuildMake register() rebuild even if the menu already exists.
cacheCache the built structure (see below). true, a string key, or ['key' => ..., 'config' => ...].

Any other keys (e.g. renderer, singleActive) set here become defaults for later render() calls on that menu.

Caching a built menu

register() with a cache option builds the menu through the callback once and caches its structure (not its active state); later requests skip the build and load the tree from cache, while active state is always resolved fresh per request. Pass rebuild => true to refresh.

php
$this->Menu->register('main', function ($menu): void {
    // ...expensive build, e.g. a DB/ACL query...
}, ['cache' => 'menu_main']); // or ['cache' => true] (keyed by menu name), or ['key' => ..., 'config' => ...]

WARNING

The cached form is the serialized array, so custom item classes are restored as the base item class. Cache data-driven menus, not menus that rely on custom ItemInterface implementations.

Config-defined menus

The helper auto-registers menus declared in Configure::read('Menu.menus') (each value a Menu::fromArray() spec keyed by name), so they render without any wiring:

php
// config/app.php (or a dedicated config/menu.php loaded via Configure::load)
'Menu' => [
    'menus' => [
        'main' => [
            'attributes' => ['class' => 'nav'],
            'items' => [
                ['label' => 'Home', 'link' => '/'],
                ['label' => 'Articles', 'link' => ['controller' => 'Articles', 'action' => 'index']],
            ],
        ],
    ],
],
php
echo $this->Menu->render('main'); // no create()/register() needed

An explicit create()/register() of the same name overrides the configured menu entirely.

OptionDefaultDescription
linkCurrentfalseLink the current (last) crumb too.
resetBreadcrumbstrueReset Cake's Breadcrumbs helper before populating.
rendererSet to BreadcrumbRenderer::class to render directly instead of via Cake's helper.

Per-item breadcrumb attributes can be set via the item's data['breadcrumbOptions'].

Released under the MIT License.