{"record":{"id":"6dbd5c6f50e486e2","repo":"symfony/routing","slug":"the-routing-file-s-must-not-specify-both-the-controller-key","errorCode":null,"errorMessage":"The routing file \"%s\" must not specify both the \"controller\" key and the defaults key \"_controller\" for \"%s\".","messagePattern":"The routing file \"(.+?)\" must not specify both the \"controller\" key and the defaults key \"_controller\" for \"(.+?)\"\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"Loader/ContentLoaderTrait.php","lineNumber":229,"sourceCode":"        if (isset($config['alias'])) {\n            $this->validateAlias($config, $name, $path);\n\n            return;\n        }\n        if ($extraKeys = array_diff(array_keys($config), self::AVAILABLE_KEYS)) {\n            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)));\n        }\n        if (isset($config['resource']) && isset($config['path'])) {\n            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));\n        }\n        if (!isset($config['resource']) && isset($config['type'])) {\n            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));\n        }\n        if (!isset($config['resource']) && !isset($config['path'])) {\n            throw new \\InvalidArgumentException(\\sprintf('You must define a \"path\" for the route \"%s\" in file \"%s\".', $name, $path));\n        }\n        if (isset($config['controller']) && isset($config['defaults']['_controller'])) {\n            throw new \\InvalidArgumentException(\\sprintf('The routing file \"%s\" must not specify both the \"controller\" key and the defaults key \"_controller\" for \"%s\".', $path, $name));\n        }\n        if (isset($config['stateless']) && isset($config['defaults']['_stateless'])) {\n            throw new \\InvalidArgumentException(\\sprintf('The routing file \"%s\" must not specify both the \"stateless\" key and the defaults key \"_stateless\" for \"%s\".', $path, $name));\n        }\n        if (isset($config['firewall']) && isset($config['defaults']['_firewall'])) {\n            throw new \\InvalidArgumentException(\\sprintf('The routing file \"%s\" must not specify both the \"firewall\" key and the defaults key \"_firewall\" for \"%s\".', $path, $name));\n        }\n    }\n\n    /**\n     * Validates that an alias definition only carries the `alias` and `deprecated` keys.\n     *\n     * @throws \\InvalidArgumentException If one of the provided config keys is not supported,\n     *                                   something is missing or the combination is nonsense\n     */\n    private function validateAlias(array $config, string $name, string $path): void\n    {\n        foreach ($config as $key => $value) {","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/symfony/routing/blob/83fa223250b50f4f018c011e101c330e65ac63cc/Loader/ContentLoaderTrait.php#L211-L247","documentation":"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.","triggerScenarios":"A route entry in a routing file that defines both 'controller: ...' and 'defaults: { _controller: ... }' for the same route name.","commonSituations":"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.","solutions":["Remove one of the two declarations — prefer the modern shorthand 'controller' key and delete defaults['_controller']","Or keep only defaults['_controller'] if you must target an older Symfony style","Search the routing file for duplicate controller keys after merges"],"exampleFix":"# before\napp_blog:\n    path: /blog\n    controller: App\\Controller\\BlogController::index\n    defaults:\n        _controller: App\\Controller\\BlogController::index\n\n# after\napp_blog:\n    path: /blog\n    controller: App\\Controller\\BlogController::index","handlingStrategy":"validation","validationCode":"if (isset($config['controller']) && isset($config['defaults']['_controller'])) {\n    throw new \\LogicException('Use either \"controller\" or defaults._controller, not both.');\n}","typeGuard":"function hasSingleController(array $config): bool { return !(isset($config['controller']) && isset($config['defaults']['_controller'])); }","tryCatchPattern":"try { $collection = $loader->load($file, 'yaml'); } catch (\\InvalidArgumentException $e) { /* duplicate controller declaration; dedupe config */ }","preventionTips":["Standardize on the 'controller' shorthand key","Grep for '_controller' in modern routing files","Resolve merge conflicts by keeping only one controller declaration"],"tags":["symfony","routing","config-validation"],"backgroundTag":"conflicting-config-options","analyzedSha":"83fa223250b50f4f018c011e101c330e65ac63cc","analyzedAt":"2026-09-14T03:19:46.280Z","contentChangedAt":"2026-09-14T03:19:46.280Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}