symfony/routing · error · RouteNotFoundException

Unable to generate a URL for the named route

Error message

Unable to generate a URL for the named route "%s" as such route does not exist.

What it means

Thrown by generate() when the requested route name (after optional _locale-suffix resolution against compiledRoutes) has no entry in the compiled route table. This is a sentinel guard for unknown route names: the caller passed a $name that was never registered, or only exists under a different locale-suffixed variant that fails the _canonical_route check, or the routes were dumped from a different route collection than the one this generator was built from. Fix by generating only names present in the loaded route collection.

Solutions

  1. Verify the route name with `php bin/console debug:router`.
  2. Fix the typo / use the correct route name in the generate() call.
  3. Rebuild the cache (`php bin/console cache:clear`) so the compiled routes include the route.
  4. Wrap generation in try/catch on RouteNotFoundException to fall back gracefully.
  5. For localized routes, ensure the route exists for the target locale or add a fallback.

Example fix

// before
$url = $generator->generate('app_home');
// after
try {
    $url = $generator->generate('app_home');
} catch (RouteNotFoundException $e) {
    $url = '/';
}
Defensive patterns

Strategy: try-catch

Validate before calling

// before generate: check compiled routes if accessible, or maintain a route-name allowlist
if (!in_array($name, $knownRouteNames, true)) {
    return null;
}

Type guard

function routeExists(object $router, string $name): bool {
    $routes = $router->getRouteCollection()->all();
    return isset($routes[$name]) || array_key_exists($name, $routes);
}

Try / catch

use Symfony\Component\Routing\Exception\RouteNotFoundException;
try { $url = $generator->generate($name, $params); } catch (RouteNotFoundException $e) { $url = null; }

Prevention

When it happens

Trigger: Calling UrlGeneratorInterface::generate($name) (or ->generate on CompiledUrlGenerator) with a $name absent from $this->compiledRoutes, including after localized-route fallback by locale prefix failed.

Common situations: Typo in route name; route defined in a bundle not loaded; route removed after a refactor or cache not warmed; generating a localized route missing for the current locale.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at Generator/CompiledUrlGenerator.php:52

    }

    public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string
    {
        $locale = $parameters['_locale']
            ?? $this->context->getParameter('_locale')
            ?: $this->defaultLocale;

        if (null !== $locale) {
            do {
                if (($this->compiledRoutes[$name.'.'.$locale][1]['_canonical_route'] ?? null) === $name) {
                    $name .= '.'.$locale;
                    break;
                }
            } while (false !== $locale = strstr($locale, '_', true));
        }

        if (!isset($this->compiledRoutes[$name])) {
            throw new RouteNotFoundException(\sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
        }

        [$variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes, $deprecations] = $this->compiledRoutes[$name] + [6 => []];

        foreach ($deprecations as $deprecation) {
            trigger_deprecation($deprecation['package'], $deprecation['version'], $deprecation['message']);
        }

        if (isset($defaults['_canonical_route']) && isset($defaults['_locale'])) {
            if (!\in_array('_locale', $variables, true)) {
                unset($parameters['_locale']);
            } elseif (!isset($parameters['_locale'])) {
                $parameters['_locale'] = $defaults['_locale'];
            }
        }

        return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
    }

View on GitHub (pinned to 83fa223250)