yiisoft/yii2 · error · InvalidConfigException

The "id" configuration for the Application is required.

Error message

The "id" configuration for the Application is required.

What it means

yii\base\Application::preInit() runs at the very start of the constructor and enforces two mandatory config keys, checked in order: 'id' then 'basePath'. A missing 'id' throws InvalidConfigException immediately - every Yii application requires a unique id, used to distinguish assets, caches, and sessions when several apps run side by side.

Source

Thrown at framework/base/Application.php:218

        $this->preInit($config);

        $this->registerErrorHandler($config);

        Component::__construct($config);
    }

    /**
     * Pre-initializes the application.
     * This method is called at the beginning of the application constructor.
     * It initializes several important application properties.
     * If you override this method, please make sure you call the parent implementation.
     * @param array $config the application configuration
     * @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing.
     */
    public function preInit(&$config)
    {
        if (!isset($config['id'])) {
            throw new InvalidConfigException('The "id" configuration for the Application is required.');
        }
        if (isset($config['basePath'])) {
            $this->setBasePath($config['basePath']);
            unset($config['basePath']);
        } else {
            throw new InvalidConfigException('The "basePath" configuration for the Application is required.');
        }

        if (isset($config['vendorPath'])) {
            $this->setVendorPath($config['vendorPath']);
            unset($config['vendorPath']);
        } else {
            // set "@vendor"
            $this->getVendorPath();
        }
        if (isset($config['runtimePath'])) {
            $this->setRuntimePath($config['runtimePath']);
            unset($config['runtimePath']);

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Add 'id' => 'myapp' to the config array passed to the Application constructor
  2. Verify which config file the entry script includes - the throw proves the loaded array really lacks the key
  3. Use distinct ids per entry point (app-frontend, app-console) in multi-app setups

Example fix

// before
new \yii\console\Application(['basePath' => __DIR__, 'components' => []]);

// after
new \yii\console\Application(['id' => 'console', 'basePath' => __DIR__, 'components' => []]);
Defensive patterns

Strategy: validation

Validate before calling

foreach (['id', 'basePath'] as $key) {
    if (!isset($config[$key])) {
        throw new \InvalidArgumentException("App config is missing '{$key}'");
    }
}
$app = new \yii\console\Application($config);

Try / catch

try {
    $app = new \yii\web\Application($config);
} catch (\yii\base\InvalidConfigException $e) {
    error_log('App config incomplete, loaded file: ' . $configFile);
    throw $e;
}

Prevention

When it happens

Trigger: new \yii\web\Application($config) or the console Application with a config array lacking 'id'; booting a minimal application for a microservice or test bootstrap; an entry script that includes an empty or wrong config file (the include returned an empty array).

Common situations: Test harnesses constructing the application from a slimmed config; splitting config into web.php/console.php and forgetting 'id' in one variant; a config file that conditionally returns early so keys are dropped.

Related errors


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