{"record":{"id":"cc169b5fc4307273","repo":"slimphp/Slim","slug":"named-route-does-not-exist-for-name-s","errorCode":null,"errorMessage":"Named route does not exist for name: %s","messagePattern":"Named route does not exist for name: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"Slim/Routing/RouteCollector.php","lineNumber":213,"sourceCode":"    public function getNamedRoute(string $name): RouteInterface\n    {\n        if (isset($this->routesByName[$name])) {\n            $route = $this->routesByName[$name];\n            if ($route->getName() === $name) {\n                return $route;\n            }\n\n            unset($this->routesByName[$name]);\n        }\n\n        foreach ($this->routes as $route) {\n            if ($name === $route->getName()) {\n                $this->routesByName[$name] = $route;\n                return $route;\n            }\n        }\n\n        throw new RuntimeException('Named route does not exist for name: ' . $name);\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function lookupRoute(string $identifier): RouteInterface\n    {\n        if (!isset($this->routes[$identifier])) {\n            throw new RuntimeException('Route not found, looks like your route cache is stale.');\n        }\n        return $this->routes[$identifier];\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function group(string $pattern, $callable): RouteGroupInterface\n    {","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/slimphp/Slim/blob/80900fb39cafce3ae53b18a2c4f642a122f03095/Slim/Routing/RouteCollector.php#L195-L231","documentation":"RouteCollector::getNamedRoute() is the lookup behind RouteParser::urlFor()/fullUrlFor() and every 'generate URL by name' API. It checks the routesByName cache, then falls back to a linear scan of all registered routes; if no route carries the requested name it throws this RuntimeException. It means that, at the moment of the call, no route registered via ->setName() (or ->setName() on the result of map/get/post/...) matches the given string exactly.","triggerScenarios":"Calling $routeParser->urlFor('user.profile', [...]) (from RouteContext::fromRequest($request)->getRouteParser() or $app->getRouteCollector()->getRouteParser()) with a name that was never set, is misspelled, or has different casing. Also triggered when the route file defining the name has not been loaded yet, or when a route cache file was generated before the named route existed (lookup scans in-memory routes only, not the dispatcher cache).","commonSituations":"Typos and case mismatches ('UserProfile' vs 'user.profile'); renaming a route in definitions but not at call sites; URL generation happening in middleware or bootstrap before all route definition files are required; stale route cache after adding new named routes; expecting group patterns to auto-prefix route names (Slim does not — names must be set explicitly per route); porting from Slim 3 pathFor() with old names.","solutions":["Verify the name matches exactly (case-sensitive) the value passed to ->setName() on that route","Ensure the route definition file is loaded before urlFor() runs (check require/include order in bootstrap)","If route caching is enabled, delete the cache file after adding or renaming routes so it is rebuilt","Guard call sites with a hasNamedRoute() check over $app->getRouteCollector()->getRoutes() and fail with a clear message listing known names"],"exampleFix":"// before\n$url = $routeParser->urlFor('user.profil', ['id' => 42]); // typo, name never registered\n\n// after\n$app->get('/user/{id:[0-9]+}', UserProfileAction::class)->setName('user.profile');\n$url = $routeParser->urlFor('user.profile', ['id' => 42]);","handlingStrategy":"validation","validationCode":"function hasNamedRoute(Slim\\Routing\\RouteCollector $collector, string $name): bool\n{\n    foreach ($collector->getRoutes() as $route) {\n        if ($route->getName() === $name) {\n            return true;\n        }\n    }\n    return false;\n}\n\n// before generating:\nif (!hasNamedRoute($app->getRouteCollector(), 'user.profile')) {\n    throw new RuntimeException(\"Route name 'user.profile' is not registered — check ->setName() and route loading order\");\n}\n$url = $app->getRouteCollector()->getRouteParser()->urlFor('user.profile', ['id' => 42]);","typeGuard":null,"tryCatchPattern":"try {\n    $url = $routeParser->urlFor($name, $data);\n} catch (RuntimeException $e) {\n    // message contains the unknown name; log with known names for quick diagnosis\n    $log->warning($e->getMessage(), ['known' => array_map(fn($r) => $r->getName(), $collector->getRoutes())]);\n    $url = '/'; // or rethrow as a domain-specific exception with context\n}","preventionTips":["Define route names as class constants or a central enum and reference them everywhere instead of string literals","Load all route definition files in bootstrap before anything that can generate URLs","Regenerate the route cache whenever route names change (include it in the deploy checklist)","Add a start-up assertion that every name used by link-builders exists in getRoutes()"],"tags":["slim","php","routing","url-generation","route-name"],"backgroundTag":"unknown-route-name","analyzedSha":"80900fb39cafce3ae53b18a2c4f642a122f03095","analyzedAt":"2026-08-21T01:41:09.580Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}