symfony/routing · error · InvalidArgumentException

The routing file " " must not specify both the "controller"…

Error message

The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".

What it means

The controller for a route may be declared either with the shorthand 'controller' key or via defaults['_controller'], but not both — they would be contradictory. ContentLoaderTrait::validate() throws InvalidArgumentException when both are present in the same route entry.

Solutions

  1. Remove one of the two declarations — prefer the modern shorthand 'controller' key and delete defaults['_controller']
  2. Or keep only defaults['_controller'] if you must target an older Symfony style
  3. Search the routing file for duplicate controller keys after merges

Example fix

# before
app_blog:
    path: /blog
    controller: App\Controller\BlogController::index
    defaults:
        _controller: App\Controller\BlogController::index

# after
app_blog:
    path: /blog
    controller: App\Controller\BlogController::index
Defensive patterns

Strategy: validation

Validate before calling

if (isset($config['controller']) && isset($config['defaults']['_controller'])) {
    throw new \LogicException('Use either "controller" or defaults._controller, not both.');
}

Type guard

function hasSingleController(array $config): bool { return !(isset($config['controller']) && isset($config['defaults']['_controller'])); }

Try / catch

try { $collection = $loader->load($file, 'yaml'); } catch (\InvalidArgumentException $e) { /* duplicate controller declaration; dedupe config */ }

Prevention

When it happens

Trigger: A route entry in a routing file that defines both 'controller: ...' and 'defaults: { _controller: ... }' for the same route name.

Common situations: Merging two config files or branches that each set the controller differently; IDE auto-completion adding 'controller' while '_controller' already existed in defaults; copy-paste from older Symfony configs using the defaults style.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


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

Appendix: source

Thrown at Loader/ContentLoaderTrait.php:229

        if (isset($config['alias'])) {
            $this->validateAlias($config, $name, $path);

            return;
        }
        if ($extraKeys = array_diff(array_keys($config), self::AVAILABLE_KEYS)) {
            throw new \InvalidArgumentException(\sprintf('The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".', $path, $name, implode('", "', $extraKeys), implode('", "', self::AVAILABLE_KEYS)));
        }
        if (isset($config['resource']) && isset($config['path'])) {
            throw new \InvalidArgumentException(\sprintf('The routing file "%s" must not specify both the "resource" key and the "path" key for "%s". Choose between an import and a route definition.', $path, $name));
        }
        if (!isset($config['resource']) && isset($config['type'])) {
            throw new \InvalidArgumentException(\sprintf('The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.', $name, $path));
        }
        if (!isset($config['resource']) && !isset($config['path'])) {
            throw new \InvalidArgumentException(\sprintf('You must define a "path" for the route "%s" in file "%s".', $name, $path));
        }
        if (isset($config['controller']) && isset($config['defaults']['_controller'])) {
            throw new \InvalidArgumentException(\sprintf('The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".', $path, $name));
        }
        if (isset($config['stateless']) && isset($config['defaults']['_stateless'])) {
            throw new \InvalidArgumentException(\sprintf('The routing file "%s" must not specify both the "stateless" key and the defaults key "_stateless" for "%s".', $path, $name));
        }
        if (isset($config['firewall']) && isset($config['defaults']['_firewall'])) {
            throw new \InvalidArgumentException(\sprintf('The routing file "%s" must not specify both the "firewall" key and the defaults key "_firewall" for "%s".', $path, $name));
        }
    }

    /**
     * Validates that an alias definition only carries the `alias` and `deprecated` keys.
     *
     * @throws \InvalidArgumentException If one of the provided config keys is not supported,
     *                                   something is missing or the combination is nonsense
     */
    private function validateAlias(array $config, string $name, string $path): void
    {
        foreach ($config as $key => $value) {

View on GitHub (pinned to 83fa223250)