{"record":{"id":"3468f9a576f0d16a","repo":"phalcon/cphalcon","slug":"cannot-obtain-a-route-using-the-name-routename","errorCode":null,"errorMessage":"Cannot obtain a route using the name '{routeName}'","messagePattern":"Cannot obtain a route using the name '(.+?)'","errorType":"exception","errorClass":"Phalcon\\Mvc\\Url\\Exceptions\\RouteNotFound","httpStatus":null,"severity":"error","filePath":"phalcon/Mvc/Url.zep","lineNumber":176,"sourceCode":"                if unlikely typeof container != \"object\" {\n                    throw new RouterServiceUnavailable();\n                }\n\n                if unlikely !container->has(\"router\") {\n                    throw new RouterServiceUnavailable();\n                }\n\n                let router       = <RouterInterface> container->getShared(\"router\"),\n                    this->router = router;\n            }\n\n            /**\n             * Every route is uniquely differenced by a name\n             */\n            let route = <RouteInterface> router->getRouteByName(routeName);\n\n            if unlikely typeof route != \"object\" {\n                throw new RouteNotFound(routeName);\n            }\n\n            /**\n             * Replace the patterns by its variables\n             */\n            let uri = phalcon_replace_paths(\n                route->getPattern(),\n                route->getReversedPaths(),\n                uri\n            );\n\n            /**\n             * If the route has a hostname restriction, prepend it as a\n             * protocol-relative URL so the generated link works under\n             * both HTTP and HTTPS.  The baseUri is not prepended in this\n             * case because the hostname already provides the authority.\n             */\n            let hostname = route->getHostname();","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Mvc/Url.zep#L158-L194","documentation":"Url::get(['for' => $name]) resolves the name through Router::getRouteByName() (phalcon/Mvc/Url.zep:176). If the router returns nothing for that name — no route was ever named $routeName — RouteNotFound is thrown with the offending name interpolated into the message. Named-route generation requires routes explicitly named via setName()/['name' => ...] in the loaded definition.","triggerScenarios":"Typo between 'for' value and the ->setName() value; routes defined in a module/controller that was never mounted; generating URLs before the routes file was loaded into the router; mixed case ('Posts-show' vs 'posts-show').","commonSituations":"Route defined but never named (names are not auto-generated); route definitions in a not-yet-loaded module or deferred provider; renaming a route during refactor while templates/Volt {{ url() }} calls still use the old name; routes added via annotations where the annotation loader did not run.","solutions":["Verify the exact name: $router->getRouteByName('posts-show') should return a RouteInterface before Url::get() is called","Add ->setName('posts-show') (or 'name' in the array definition) to the intended route","Ensure the routes are actually mounted/loaded before URL generation (check route order in bootstrap and module definitions)"],"exampleFix":"// before\n$router->add('/post/{id}', ['controller' => 'posts', 'action' => 'show']);\n$url->get(['for' => 'post-show', 'id' => 7]); // route never named\n\n// after\n$router->add('/post/{id}', ['controller' => 'posts', 'action' => 'show'])\n       ->setName('post-show');\n$url->get(['for' => 'post-show', 'id' => 7]);","handlingStrategy":"try-catch","validationCode":"if ($router->getRouteByName($routeName) === null) {\n    throw new \\InvalidArgumentException(\"No route named '{$routeName}' is registered\");\n}","typeGuard":null,"tryCatchPattern":"use Phalcon\\Mvc\\Url\\Exceptions\\RouteNotFound;\ntry {\n    $link = $url->get(['for' => $routeName] + $params);\n} catch (RouteNotFound $e) {\n    $link = $url->get('/fallback/path', $params); // or log and rethrow\n}","preventionTips":["Name every route you intend to generate URLs for","Assert route names exist in a bootstrap check: iterate required names over getRouteByName()","Keep route-name constants in one class to avoid typos","Clear route caches and reload modules before URL generation"],"tags":["phalcon","url","router","named-routes","typo"],"backgroundTag":"route-not-found","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}