yiisoft/yii2 · error · InvalidConfigException
Unknown component ID: $id
Error message
Unknown component ID: $id
What it means
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.
Source
Thrown at framework/di/ServiceLocator.php:140
* @throws InvalidConfigException if `$id` refers to a nonexistent component ID
* @see has()
* @see set()
*/
public function get($id, $throwException = true)
{
if (isset($this->_components[$id])) {
return $this->_components[$id];
}
if (isset($this->_definitions[$id])) {
$definition = $this->_definitions[$id];
if (is_object($definition) && !$definition instanceof Closure) {
return $this->_components[$id] = $definition;
}
return $this->_components[$id] = Yii::createObject($definition);
} elseif ($throwException) {
throw new InvalidConfigException("Unknown component ID: $id");
}
return null;
}
/**
* Registers a component definition with this locator.
*
* For example,
*
* ```
* // a class name
* $locator->set('cache', 'yii\caching\FileCache');
*
* // a configuration array
* $locator->set('db', [
* 'class' => 'yii\db\Connection',
* 'dsn' => 'mysql:host=127.0.0.1;dbname=demo',View on GitHub (pinned to 66f00d18a2)
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.
Example fix
// before
$queue = Yii::$app->get('queue'); // not configured -> Unknown component ID
// after
// config: 'components' => ['queue' => ['class' => yii\queue\file\Queue::class]]
$queue = Yii::$app->has('queue') ? Yii::$app->get('queue') : null; Defensive patterns
Strategy: validation
Validate before calling
if (Yii::$app->has('queue')) {
$queue = Yii::$app->get('queue');
} else {
$queue = null; // or lazy-register a default
}
// alternative non-throwing lookup:
$queue = Yii::$app->get('queue', false); // null when absent Try / catch
try {
$component = Yii::$app->get('queue');
} catch (\yii\base\InvalidConfigException $e) {
if (strpos($e->getMessage(), 'Unknown component ID') === 0) {
// register a default and retry once
Yii::$app->set('queue', ['class' => yii\queue\file\Queue::class]);
$component = Yii::$app->get('queue');
} else {
throw $e;
}
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The configuration for the "$id" component must contain a "cl
- Unexpected configuration type for the "$id" component: getty
- Invalid validation rule: a rule must specify both attribute
- The directory does not exist: $path
- {cipher} is not an allowed cipher
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/dc9c1fcd32cf4d9a.
Report an issue: GitHub.