Symfony 6: how to register custom URL matcher

307 Views Asked by At

For some reason I need to register custom URL matcher in Symfony 6 but I'm getting weird error that my service doesn't exist:

Service "App\Routing\QueryStringUrlMatcher" not found: the container inside "Symfony\Component\DependencyInjection\Argument\ServiceLocator" is a smaller service locator that only knows about the "kernel" service.

Custom matcher class:

namespace App\Routing;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouterInterface;

class QueryStringUrlMatcher extends UrlMatcher
{
    public function __construct(RouterInterface $router, RequestContext $context)
    {
        parent::__construct($router->getRouteCollection(), $context);
    }

    public function matchRequest(Request $request): array
    {
        $this->request = $request;

        $ret = $this->match($request->getRequestUri());

        $this->request = null;

        return $ret;
    }
}

config/services.yml:

services:
    url_matcher:
        class: App\Routing\QueryStringUrlMatcher
        arguments:
            - '@router.default'
            - '@router.request_context'
        tags:
            - { name: router.matcher }

config/packages/framework.yml:

framework:
    router:
        resource: App\Routing\QueryStringUrlMatcher
        type: service
0

There are 0 best solutions below