{"record":{"id":"b850bf300cdd0200","repo":"phalcon/cphalcon","slug":"a-dependency-injection-container-is-required-to-ac-b850bf","errorCode":null,"errorMessage":"A dependency injection container is required to access the 'url' service","messagePattern":"A dependency injection container is required to access the 'url' service","errorType":"exception","errorClass":"Phalcon\\Tag\\Exception","httpStatus":null,"severity":"error","filePath":"phalcon/Tag.zep","lineNumber":507,"sourceCode":"    public static function getTitleSeparator() -> string\n    {\n        return (string) self::documentTitleSeparator;\n    }\n\n    /**\n     * Returns a URL service from the default DI\n     */\n    public static function getUrlService() -> <UrlInterface>\n    {\n        var url, container;\n\n        let url = self::urlService;\n\n        if typeof url != \"object\" {\n            let container = self::getDI();\n\n            if container === null {\n                throw new Exception(\n                    \"A dependency injection container is required to access the 'url' service\"\n                );\n            }\n\n            let url = <UrlInterface> container->getShared(\"url\"),\n                self::urlService = url;\n        }\n\n        return url;\n    }\n\n    /**\n     * Every helper calls this function to check whether a component has a\n     * predefined value using Phalcon\\Tag::setDefault() or value from $_POST\n     *\n     * @phpstan-param tag_parameters $parameters\n     */\n    public static function getValue(var name, array params = []) -> mixed","sourceCodeStart":489,"sourceCodeEnd":525,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Tag.zep#L489-L525","documentation":"Phalcon\\Tag's URL-related helpers (linkTo, image, stylesheets etc.) resolve the 'url' shared service from the default DI container; the resolved UrlInterface is cached statically in Tag::$urlService. When no default container exists, getUrlService() throws Phalcon\\Tag\\Exception (\"A dependency injection container is required to access the 'url' service\"). FactoryDefault registers 'url' (Phalcon\\Mvc\\Url) automatically — hitting this means Tag ran without a booted container.","triggerScenarios":"Calling Tag::linkTo() / Tag::image() / Tag::stylesheetLink() in a unit test or CLI script; after Di::reset(); once Tag::$urlService has been cached by an earlier successful call, later calls no longer need the container (order-dependent).","commonSituations":"Testing view helpers in isolation; micro-apps or workers that render partials; custom bootstrap that never registers the container or calls Di::setDefault.","solutions":["Register a container: Tag::setDI($di) with $di->setShared('url', \\Phalcon\\Mvc\\Url::class), or Di::setDefault(new FactoryDefault())","Register 'url' explicitly if you use a bare Phalcon\\Di\\Di instead of FactoryDefault","Keep tag helper usage inside the bootstrapped application lifecycle"],"exampleFix":"// before (no container)\necho \\Phalcon\\Tag::linkTo(['https://example.com', 'Example']); // throws\n\n// after\n$di = new \\Phalcon\\Di\\Di();\n$di->setShared('url', \\Phalcon\\Mvc\\Url::class);\n\\Phalcon\\Di\\Di::setDefault($di);\n\\Phalcon\\Tag::setDI($di);\n\necho \\Phalcon\\Tag::linkTo(['https://example.com', 'Example']);","handlingStrategy":"validation","validationCode":"use Phalcon\\Di\\Di;\nuse Phalcon\\Mvc\\Url;\nuse Phalcon\\Tag;\n\nif (null === Di::getDefault()) {\n    $di = new Di();\n    $di->setShared('url', Url::class);\n    Di::setDefault($di);\n    Tag::setDI($di);\n}\n\necho \\Phalcon\\Tag::linkTo(['https://example.com', 'Example']);","typeGuard":"function hasTagUrlContainer(): bool\n{\n    return null !== \\Phalcon\\Di\\Di::getDefault()\n        || null !== \\Phalcon\\Tag::getDI();\n}","tryCatchPattern":"try {\n    echo \\Phalcon\\Tag::linkTo(['user/list', 'Users']);\n} catch (\\Phalcon\\Tag\\Exception $e) {\n    if (str_contains($e->getMessage(), \"'url' service\")) {\n        echo '<a href=\"/user/list\">Users</a>'; // manual fallback\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Register 'url' (Phalcon\\Mvc\\Url) in any container used with Tag helpers","Keep tag rendering inside the bootstrapped application; avoid Tag in container-less scripts","Reset static state (Di::reset) between tests so cached services do not mask a missing container"],"tags":["phalcon","tag","dependency-injection","url"],"backgroundTag":"missing-di-container","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}