Skip to main content
The menu code resolver determines the effective menu code used to look up a menu from the database. The default resolver returns the hint unchanged — so dashboard_menu_tree('sidebar') always loads the menu with code sidebar. Implement a custom resolver when you need to select different menu variants based on the current request, for example by operator ID, partner ID, or any other request attribute.

The interface

namespace Nowo\DashboardMenuBundle\Service;

use Symfony\Component\HttpFoundation\Request;

interface MenuCodeResolverInterface
{
    /**
     * Resolve the menu code to use for loading the tree.
     *
     * @param string $hint The requested menu name/code from the template or API (e.g. "sidebar")
     */
    public function resolveMenuCode(Request $request, string $hint): string;
}

Implementing a resolver

The example below tries increasingly general menu codes, falling back to the plain hint when no specific variant exists:
src/Service/MyMenuCodeResolver.php
<?php

declare(strict_types=1);

namespace App\Service;

use Nowo\DashboardMenuBundle\Repository\MenuRepository;
use Nowo\DashboardMenuBundle\Service\MenuCodeResolverInterface;
use Symfony\Component\HttpFoundation\Request;

final class MyMenuCodeResolver implements MenuCodeResolverInterface
{
    public function __construct(
        private readonly MenuRepository $menuRepository,
    ) {
    }

    public function resolveMenuCode(Request $request, string $hint): string
    {
        $operatorId = $request->attributes->getInt('operatorId', 0);
        $partnerId  = $request->attributes->getInt('partnerId', 0);

        // First try: operator + partner + name
        if ($operatorId > 0 && $partnerId > 0) {
            $code = sprintf('op_%d_partner_%d_%s', $operatorId, $partnerId, $hint);
            if ($this->menuRepository->findOneByCode($code) !== null) {
                return $code;
            }
        }

        // Then: partner + name
        if ($partnerId > 0) {
            $code = sprintf('partner_%d_%s', $partnerId, $hint);
            if ($this->menuRepository->findOneByCode($code) !== null) {
                return $code;
            }
        }

        // Fallback: name only
        return $hint;
    }
}

Registering the resolver

1

Implement the interface

Create your class in src/ and implement MenuCodeResolverInterface. Symfony auto-wires it through the normal service discovery.
2

Alias the interface in services.yaml

The bundle wires MenuCodeResolverInterface to DefaultMenuCodeResolver by default. Override that alias in your app’s services.yaml to point to your class:
config/services.yaml
services:
    Nowo\DashboardMenuBundle\Service\MenuCodeResolverInterface:
        alias: App\Service\MyMenuCodeResolver
There is no menu_code_resolver key in nowo_dashboard_menu.yaml. Registration is done entirely through the Symfony service container by replacing the MenuCodeResolverInterface alias.

Where the resolver is used

The resolver runs on every menu load, in both Twig and the JSON API:
CallerExample
Twig functiondashboard_menu_tree('sidebar') → resolver receives hint = 'sidebar'
JSON APIGET /api/menu/sidebar → resolver receives hint = 'sidebar'
The resolved code is then used to look up the menu (and apply context sets if provided).

Default behaviour

DefaultMenuCodeResolver returns the hint unchanged:
final class DefaultMenuCodeResolver implements MenuCodeResolverInterface
{
    public function resolveMenuCode(Request $request, string $hint): string
    {
        return $hint;
    }
}
This is what the bundle uses when no custom resolver is configured.