{"record":{"id":"b882c786174e6dc5","repo":"phalcon/cphalcon","slug":"a-dependency-injection-container-is-required-to-ac-b882c7","errorCode":null,"errorMessage":"A dependency injection container is required to access the 'router' service","messagePattern":"A dependency injection container is required to access the 'router' service","errorType":"exception","errorClass":"Phalcon\\Mvc\\Url\\Exceptions\\RouterServiceUnavailable","httpStatus":null,"severity":"error","filePath":"phalcon/Mvc/Url.zep","lineNumber":159,"sourceCode":"        if typeof baseUri != \"string\" {\n            let baseUri = this->getBaseUri();\n        }\n\n        if typeof uri == \"array\" {\n            if unlikely !fetch routeName, uri[\"for\"] {\n                throw new MissingRouteName();\n            }\n\n            let router = this->router;\n\n            /**\n             * Check if the router has not previously set\n             */\n            if unlikely !router {\n                let container = <DiInterface> this->container;\n\n                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            }","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Mvc/Url.zep#L141-L177","documentation":"When Url::get() resolves a named route and no Router was injected directly, the Url component falls back to its DI container (phalcon/Mvc/Url.zep:159). If no container object is attached at all, RouterServiceUnavailable is thrown: named-route URL generation requires either a Router or a container to fetch one from.","triggerScenarios":"Using $url = new \\Phalcon\\Mvc\\Url() standalone without calling setDI()/setContainer(); using Url inside a CLI worker, queue job, or unit test where the app DI was never attached; copying Url into a service that bypasses the shared container.","commonSituations":"Unit tests constructing Url manually; long-running workers (cron, queue consumers) that new up components instead of resolving from DI; refactoring code out of a request cycle and losing the container reference.","solutions":["Attach the container: $url->setDI($di) (or setContainer($di)) before generating named-route URLs","Or bypass DI entirely by injecting the router: $url->setRouter($router)","In CLI/test bootstrap, reuse the same DI setup as the web app"],"exampleFix":"// before\n$url = new \\Phalcon\\Mvc\\Url();\necho $url->get(['for' => 'home']); // no container -> RouterServiceUnavailable\n\n// after\n$url = new \\Phalcon\\Mvc\\Url();\n$url->setDI($di);\necho $url->get(['for' => 'home']);","handlingStrategy":"validation","validationCode":"if ($url->getDI() === null && !$url->getRouter()) {\n    throw new \\RuntimeException('Attach a DI container or router before named-route URL generation');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $link = $url->get(['for' => 'home']);\n} catch (\\Phalcon\\Mvc\\Url\\Exceptions\\RouterServiceUnavailable $e) {\n    $url->setDI($app->getDI()); // lazy-attach then retry\n    $link = $url->get(['for' => 'home']);\n}","preventionTips":["Call $url->setDI($di) right after constructing Url","In workers/tests, reuse the app bootstrap's DI","Inject the router via setRouter() when DI is unavailable"],"tags":["phalcon","url","di","router","bootstrap"],"backgroundTag":"missing-di-container","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}