{"record":{"id":"66f51f7746c769b2","repo":"symfony/routing","slug":"route-alias-s-can-not-reference-itself","errorCode":null,"errorMessage":"Route alias \"%s\" can not reference itself.","messagePattern":"Route alias \"(.+?)\" can not reference itself\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"RouteCollection.php","lineNumber":379,"sourceCode":"        $key = (string) $resource;\n\n        if (!isset($this->resources[$key])) {\n            $this->resources[$key] = $resource;\n        }\n    }\n\n    /**\n     * Sets an alias for an existing route.\n     *\n     * @param string $name  The alias to create\n     * @param string $alias The route to alias\n     *\n     * @throws InvalidArgumentException if the alias is for itself\n     */\n    public function addAlias(string $name, string $alias): Alias\n    {\n        if ($name === $alias) {\n            throw new InvalidArgumentException(\\sprintf('Route alias \"%s\" can not reference itself.', $name));\n        }\n\n        unset($this->routes[$name], $this->priorities[$name]);\n\n        return $this->aliases[$name] = new Alias($alias);\n    }\n\n    /**\n     * @return array<string, Alias>\n     */\n    public function getAliases(): array\n    {\n        return $this->aliases;\n    }\n\n    public function getAlias(string $name): ?Alias\n    {\n        return $this->aliases[$name] ?? null;","sourceCodeStart":361,"sourceCodeEnd":397,"githubUrl":"https://github.com/symfony/routing/blob/83fa223250b50f4f018c011e101c330e65ac63cc/RouteCollection.php#L361-L397","documentation":"RouteCollection::addAlias() refuses to create an alias whose name equals its target, because a self-referencing alias would be unresolvable and create an infinite lookup. The check is a plain string equality between $name and $alias.","triggerScenarios":"$collection->addAlias('app_home', 'app_home') — same string for name and target, often from dynamic/config-driven values where the two variables happen to be equal.","commonSituations":"Importing route definitions from config or a database where alias and target columns can coincide; generating aliases in a loop over route names without checking.","solutions":["Skip aliasing when name === target before calling addAlias","Fix the config/data source so the alias target points at a real different route","Guard the call: if ($name !== $target) { $collection->addAlias($name, $target); }"],"exampleFix":"// before\n$collection->addAlias($name, $target);\n// after\nif ($name !== $target) {\n    $collection->addAlias($name, $target);\n}","handlingStrategy":"validation","validationCode":"if ($name === $alias) {\n    throw new \\InvalidArgumentException(\"Alias '$name' would reference itself\");\n}\n$collection->addAlias($name, $alias);","typeGuard":null,"tryCatchPattern":"try {\n    $collection->addAlias($name, $target);\n} catch (\\InvalidArgumentException $e) {\n    // skip self-alias or log config problem\n}","preventionTips":["Check name !== target before aliasing, especially with dynamic values","Validate alias definitions from config/import pipelines","Add tests for alias generation loops"],"tags":["php","routing","alias","route-collection"],"backgroundTag":"invalid-config-value","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"}