yiisoft/yii2 · error · InvalidConfigException

The "basePath" configuration for the Application is required

Error message

The "basePath" configuration for the Application is required.

What it means

The second mandatory key enforced by Application::preInit(): 'basePath' is required in the config (there is no alias fallback in preInit itself; the value may be an alias path). setBasePath() stores it and defines the @app alias from which @runtime, @vendor and other derived aliases are built. Without the key the application cannot start and InvalidConfigException is thrown before any component initializes.

Source

Thrown at framework/base/Application.php:224

    /**
     * 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']);
        } else {
            // set "@runtime"
            $this->getRuntimePath();
        }

        if (isset($config['timeZone'])) {

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Add 'basePath' => dirname(__DIR__) - the conventional value in Yii templates - to the config
  2. Check that the entry script's require(...) loads the intended config file and that the file returns the full array
  3. If config is built programmatically, assert the required keys before constructing the app

Example fix

// before
$app = new \yii\web\Application(['id' => 'api', 'components' => require __DIR__ . '/components.php']);

// after
$app = new \yii\web\Application([
    'id' => 'api',
    'basePath' => dirname(__DIR__),
    'components' => require __DIR__ . '/components.php',
]);
Defensive patterns

Strategy: validation

Validate before calling

if (!isset($config['id'], $config['basePath'])) {
    throw new \InvalidArgumentException('Application config requires both id and basePath');
}
$app = new \yii\web\Application($config);

Try / catch

try {
    $app = new \yii\web\Application($config);
} catch (\yii\base\InvalidConfigException $e) {
    error_log('Application bootstrap failed; check config keys id/basePath in ' . $configFile);
    throw $e;
}

Prevention

When it happens

Trigger: Constructing an Application whose config has 'id' but no 'basePath'; config refactors that moved basePath into a params file; a config file that returns only part of the array; bootstrapping Yii inside queue workers or PHPUnit from a hand-written config.

Common situations: Custom micro-applications outside the standard templates; a typo or wrong case in the key ('basepath' - config keys are case-sensitive); multi-app setups where the console variant of the config lost the key.

Related errors


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