symfony/routing · error · InvalidArgumentException

Route alias " " can not reference itself.

Error message

Route alias "%s" can not reference itself.

What it means

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.

Solutions

  1. Skip aliasing when name === target before calling addAlias
  2. Fix the config/data source so the alias target points at a real different route
  3. Guard the call: if ($name !== $target) { $collection->addAlias($name, $target); }

Example fix

// before
$collection->addAlias($name, $target);
// after
if ($name !== $target) {
    $collection->addAlias($name, $target);
}
Defensive patterns

Strategy: validation

Validate before calling

if ($name === $alias) {
    throw new \InvalidArgumentException("Alias '$name' would reference itself");
}
$collection->addAlias($name, $alias);

Try / catch

try {
    $collection->addAlias($name, $target);
} catch (\InvalidArgumentException $e) {
    // skip self-alias or log config problem
}

Prevention

When it happens

Trigger: $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.

Common situations: 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.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at RouteCollection.php:379

        $key = (string) $resource;

        if (!isset($this->resources[$key])) {
            $this->resources[$key] = $resource;
        }
    }

    /**
     * Sets an alias for an existing route.
     *
     * @param string $name  The alias to create
     * @param string $alias The route to alias
     *
     * @throws InvalidArgumentException if the alias is for itself
     */
    public function addAlias(string $name, string $alias): Alias
    {
        if ($name === $alias) {
            throw new InvalidArgumentException(\sprintf('Route alias "%s" can not reference itself.', $name));
        }

        unset($this->routes[$name], $this->priorities[$name]);

        return $this->aliases[$name] = new Alias($alias);
    }

    /**
     * @return array<string, Alias>
     */
    public function getAliases(): array
    {
        return $this->aliases;
    }

    public function getAlias(string $name): ?Alias
    {
        return $this->aliases[$name] ?? null;

View on GitHub (pinned to 83fa223250)