symfony/routing · error · LogicException

You should either pass a

Error message

You should either pass a "%s" instance or provide the $parameters argument of the "%s" method.

What it means

DependencyInjection\Router needs a way to fetch route parameters: either a Psr\Container\ContainerInterface (whose $parameters is a ContainerParamFetcher-like object exposing get()) or a Symfony ContainerInterface to read parameters via getParameter(). If the $container passed is neither, a LogicException is thrown instructing to pass a PSR-11 container or provide $parameters.

Solutions

  1. Pass a Psr\Container\ContainerInterface instance (or Symfony Container) as the first constructor argument
  2. Provide the $parameters argument explicitly (a callable/fetcher with get()) instead of relying on the container
  3. Use the framework's dependency-injected router service rather than instantiating it manually

Example fix

// before
$router = new Router($somePlainObject, $loader, $routes);
// after
$router = new Router($container, $loader, $routes, [], null, null, $parameters: $container);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!$container instanceof \Psr\Container\ContainerInterface && !$container instanceof \Symfony\Component\DependencyInjection\ContainerInterface && null === $parameters) {
    throw new \LogicException('Provide a PSR-11/Symfony container or a $parameters argument');
}

Type guard

function isRouterContainer(mixed $c): bool {
    return $c instanceof \Psr\Container\ContainerInterface
        || $c instanceof \Symfony\Component\DependencyInjection\ContainerInterface;
}

Try / catch

try {
    $router = new \Symfony\Component\Routing\DependencyInjection\Router($container, $loader, $routes);
} catch (\LogicException $e) {
    // retry passing an explicit $parameters fetcher
}

Prevention

When it happens

Trigger: new Router($containerThatIsNotPsr11NorSymfonyContainer, $loader, $routes, ...) where $parameters is null and the container doesn't implement Symfony's ContainerInterface (e.g. a custom PSR-11 wrapper without the Psr\Container\ContainerInterface type, wrong container passed, or null container).

Common situations: Constructing the DI Router manually in tests or standalone apps with a homemade container; passing a PSR-11-only container that isn't Symfony's (note it must implement Psr\Container\ContainerInterface to satisfy $parameters->get), version differences after the $parameters argument was added in Symfony 6.1.

Related errors


AI-assisted analysis of symfony/routing@83fa223250 (2026-09-14). Data as JSON: /api/errors/5a4e800f88ddcaf5. Report an issue: GitHub.

Appendix: source

Thrown at DependencyInjection/Router.php:64

        private ContainerInterface $container,
        mixed $resource,
        array $options = [],
        ?RequestContext $context = null,
        ?ContainerInterface $parameters = null,
        ?LoggerInterface $logger = null,
        ?string $defaultLocale = null,
    ) {
        $this->resource = $resource;
        $this->context = $context ?? new RequestContext();
        $this->logger = $logger;
        $this->setOptions($options);

        if ($parameters) {
            $this->paramFetcher = $parameters->get(...);
        } elseif ($container instanceof SymfonyContainerInterface) {
            $this->paramFetcher = $container->getParameter(...);
        } else {
            throw new \LogicException(\sprintf('You should either pass a "%s" instance or provide the $parameters argument of the "%s" method.', SymfonyContainerInterface::class, __METHOD__));
        }

        $this->defaultLocale = $defaultLocale;
    }

    public function getRouteCollection(): RouteCollection
    {
        if (!isset($this->collection)) {
            $this->collection = $this->container->get('routing.loader')->load($this->resource, $this->options['resource_type']);
            $this->resolveParameters($this->collection);
            $this->collection->addResource(new ContainerParametersResource($this->collectedParameters));

            try {
                $containerFile = ($this->paramFetcher)('kernel.build_dir').'/'.($this->paramFetcher)('kernel.container_class').'.php';
                if (file_exists($containerFile)) {
                    $this->collection->addResource(new FileResource($containerFile));
                } else {
                    $this->collection->addResource(new FileExistenceResource($containerFile));

View on GitHub (pinned to 83fa223250)