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

  1. Register the component under that ID in the right config: 'components' => ['queue' => ['class' => yii\queue\file\Queue::class]].
  2. Check availability first with Yii::$app->has('queue') (or get('queue', false) which returns null instead of throwing).
  3. If console and web share code, define the component in a common config file included by both.
  4. 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

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


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/dc9c1fcd32cf4d9a. Report an issue: GitHub.