Skip to main content
Nowo\DashboardMenuBundle\Service\MenuCodeResolverInterface Implement this interface to resolve the effective menu code from the current request and a hint string. This lets you serve different menu variants to different tenants, operators, or partners without changing template code. The resolver is called by:
  • dashboard_menu_tree() and dashboard_menu_config() in Twig
  • GET /api/menu/{code} in the JSON API

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 (e.g. "sidebar") from the template or API
     */
    public function resolveMenuCode(Request $request, string $hint): string;
}

Method

resolveMenuCode
string
Returns the resolved menu code that the bundle will use to query the database.

Parameters

request
Request
required
The current Symfony HttpFoundation\Request. Use this to read route attributes, session data, headers, or any other request information needed to determine the correct menu code.
hint
string
required
The menu code hint as passed from the template or API path (e.g. sidebar). The resolver should fall back to returning this value unchanged when no more-specific menu is found.

Default implementation

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

Custom implementation

The following example resolves menus using operatorId and partnerId route attributes, falling back to progressively simpler codes until the hint itself:
namespace App\Menu;

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;
    }
}

Registration

The bundle registers DefaultMenuCodeResolver as the implementation of MenuCodeResolverInterface by default. Override it by aliasing the interface in your services.yaml:
config/services.yaml
services:
    Nowo\DashboardMenuBundle\Service\MenuCodeResolverInterface:
        alias: App\Menu\MyMenuCodeResolver
Your class must be registered as a Symfony service. Standard App\ resource autodiscovery covers this automatically when your class is in src/:
config/services.yaml
services:
    App\:
        resource: '../src/'

    # Override the bundle's default resolver
    Nowo\DashboardMenuBundle\Service\MenuCodeResolverInterface:
        alias: App\Menu\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.
The same resolver instance is used for both the Twig functions and the JSON API endpoint. A change in the resolver affects all rendering paths simultaneously.
Always end your resolver with a fallback that returns $hint unchanged. This ensures the bundle degrades gracefully to the base menu when no context-specific variant exists.