{"record":{"id":"a9b1904224137d2f","repo":"symfony/http-kernel","slug":"you-must-either-pass-a-requestcontext-or-the-matcher-must","errorCode":null,"errorMessage":"You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.","messagePattern":"You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"EventListener/RouterListener.php","lineNumber":62,"sourceCode":"class RouterListener implements EventSubscriberInterface\n{\n    private RequestContext $context;\n\n    /**\n     * @param RequestContext|null $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function __construct(\n        private UrlMatcherInterface|RequestMatcherInterface $matcher,\n        private RequestStack $requestStack,\n        ?RequestContext $context = null,\n        private ?LoggerInterface $logger = null,\n        private ?string $projectDir = null,\n        private bool $debug = true,\n    ) {\n        if (null === $context && !$matcher instanceof RequestContextAwareInterface) {\n            throw new \\InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.');\n        }\n\n        $this->context = $context ?? $matcher->getContext();\n    }\n\n    private function setCurrentRequest(?Request $request): void\n    {\n        if (null !== $request) {\n            try {\n                $this->context->fromRequest($request);\n            } catch (\\UnexpectedValueException $e) {\n                throw new BadRequestHttpException($e->getMessage(), $e, $e->getCode());\n            }\n        }\n    }\n\n    /**\n     * After a sub-request is done, we need to reset the routing context to the parent request so that the URL generator","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/symfony/http-kernel/blob/aa3a39d7286a62cdfea98f0e69c651a3da6e36cf/EventListener/RouterListener.php#L44-L80","documentation":"RouterListener needs a RequestContext to populate the matcher before routing. If no RequestContext was injected, the matcher must provide one via RequestContextAwareInterface; otherwise the constructor throws an InvalidArgumentException because context retrieval is impossible.","triggerScenarios":"Calling new RouterListener($matcher) where $matcher neither accepts a context nor implements RequestContextAwareInterface and no $context argument is supplied.","commonSituations":"Wiring a custom UrlMatcher/Router that only implements UrlMatcherInterface; manually constructing RouterListener in tests with a stub matcher; DIC configuration passing a bare matcher without the context argument.","solutions":["Pass a RequestContext explicitly: new RouterListener($matcher, $requestStack, $context).","Use a matcher that implements RequestContextAwareInterface (e.g. Symfony\\Component\\Routing\\Matcher\\UrlMatcher).","Extend your custom matcher from UrlMatcher or add setContext()/getContext() support."],"exampleFix":"// before\nnew RouterListener($myBareMatcher);\n// after\nnew RouterListener($myBareMatcher, null, new RequestContext());","handlingStrategy":"validation","validationCode":"if (!$matcher instanceof \\Symfony\\Component\\Routing\\Matcher\\RequestContextAwareInterface && !isset($context)) { $context = new \\Symfony\\Component\\Routing\\RequestContext(); }","typeGuard":"$matcher instanceof \\Symfony\\Component\\Routing\\Matcher\\RequestContextAwareInterface","tryCatchPattern":"try { $routerListener = new RouterListener($matcher); } catch (\\InvalidArgumentException $e) { $routerListener = new RouterListener($matcher, null, new RequestContext()); }","preventionTips":["Always pass a RequestContext explicitly when wiring RouterListener manually","Prefer framework-provided matchers that implement RequestContextAwareInterface","Type-hint construction args in DI config to catch mismatches at compile/container build time"],"tags":["routing","constructor","request-context","symfony"],"backgroundTag":"missing-required-argument","analyzedSha":"aa3a39d7286a62cdfea98f0e69c651a3da6e36cf","analyzedAt":"2026-09-13T18:03:36.509Z","contentChangedAt":"2026-09-13T18:03:36.509Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}