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
- Add 'id' => 'myapp' to the config array passed to the Application constructor
- Verify which config file the entry script includes - the throw proves the loaded array really lacks the key
- 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
- Keep one canonical config builder that always injects id and basePath
- Add a boot smoke test that constructs the application for every entry script
- Never let config files conditionally return early; always return the full array
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
- The "basePath" configuration for the Application is required
- Unknown bootstrapping component ID: $mixed
- Invalid path alias: $alias
- Unsupported configuration type: ' . gettype($type)
- Object configuration must be an array containing a "class" o
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/5334226f0f08ec6c.
Report an issue: GitHub.