{"record":{"id":"dc9c1fcd32cf4d9a","repo":"yiisoft/yii2","slug":"unknown-component-id-id","errorCode":null,"errorMessage":"Unknown component ID: $id","messagePattern":"Unknown component ID: \\$id","errorType":"exception","errorClass":"InvalidConfigException","httpStatus":null,"severity":"error","filePath":"framework/di/ServiceLocator.php","lineNumber":140,"sourceCode":"     * @throws InvalidConfigException if `$id` refers to a nonexistent component ID\n     * @see has()\n     * @see set()\n     */\n    public function get($id, $throwException = true)\n    {\n        if (isset($this->_components[$id])) {\n            return $this->_components[$id];\n        }\n\n        if (isset($this->_definitions[$id])) {\n            $definition = $this->_definitions[$id];\n            if (is_object($definition) && !$definition instanceof Closure) {\n                return $this->_components[$id] = $definition;\n            }\n\n            return $this->_components[$id] = Yii::createObject($definition);\n        } elseif ($throwException) {\n            throw new InvalidConfigException(\"Unknown component ID: $id\");\n        }\n\n        return null;\n    }\n\n    /**\n     * Registers a component definition with this locator.\n     *\n     * For example,\n     *\n     * ```\n     * // a class name\n     * $locator->set('cache', 'yii\\caching\\FileCache');\n     *\n     * // a configuration array\n     * $locator->set('db', [\n     *     'class' => 'yii\\db\\Connection',\n     *     'dsn' => 'mysql:host=127.0.0.1;dbname=demo',","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/di/ServiceLocator.php#L122-L158","documentation":"yii\\di\\ServiceLocator::get() (which Yii::$app - an Application extends ServiceLocator - uses for every component) throws 'Unknown component ID' when the requested ID is neither already instantiated nor present in the definitions array and $throwException is true (the default for get()). The locator has no fallback resolution: an unregistered ID is simply an error.","triggerScenarios":"Yii::$app->get('queue') when 'queue' is not under 'components' in the app config; Yii::$app->queue magic property access for an ID never registered via setComponents(); calling get() on a sub-module/locator where the component lives in the app config instead.","commonSituations":"Typo in a component ID ('fileCache' vs 'cache'); component defined in config/web.php but code runs in the console app (config/console.php divergence); extension components (yii2-queue, rbac) not added to config before use; accessing a module-level component through the wrong locator.","solutions":["Register the component under that ID in the right config: 'components' => ['queue' => ['class' => yii\\queue\\file\\Queue::class]].","Check availability first with Yii::$app->has('queue') (or get('queue', false) which returns null instead of throwing).","If console and web share code, define the component in a common config file included by both.","Fix ID typos by comparing against the keys in your components config."],"exampleFix":"// before\n$queue = Yii::$app->get('queue'); // not configured -> Unknown component ID\n\n// after\n// config: 'components' => ['queue' => ['class' => yii\\queue\\file\\Queue::class]]\n$queue = Yii::$app->has('queue') ? Yii::$app->get('queue') : null;","handlingStrategy":"validation","validationCode":"if (Yii::$app->has('queue')) {\n    $queue = Yii::$app->get('queue');\n} else {\n    $queue = null; // or lazy-register a default\n}\n// alternative non-throwing lookup:\n$queue = Yii::$app->get('queue', false); // null when absent","typeGuard":null,"tryCatchPattern":"try {\n    $component = Yii::$app->get('queue');\n} catch (\\yii\\base\\InvalidConfigException $e) {\n    if (strpos($e->getMessage(), 'Unknown component ID') === 0) {\n        // register a default and retry once\n        Yii::$app->set('queue', ['class' => yii\\queue\\file\\Queue::class]);\n        $component = Yii::$app->get('queue');\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Define shared components in a common config included by both web and console applications.","Use has() (or get($id, false)) before optional component access.","Add a bootstrap config test asserting that every ID referenced in code exists in Yii::$app->getComponents().","Watch for ID typos - IDs are case-sensitive strings with no autocomplete."],"tags":["yii2","service-locator","config","component","unknown-id"],"backgroundTag":"service-not-registered","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}