yiisoft/yii2 · error · InvalidConfigException

Unable to determine the entry script file path.

Error message

Unable to determine the entry script file path.

What it means

yii\base\Request::getScriptFile() lazily caches the entry script path from $_SERVER['SCRIPT_FILENAME'] and throws InvalidConfigException when that superglobal key is unset. The web Request assumes it was created inside a real HTTP request; any context that constructs or reuses the component outside one (queue workers, CLI scripts, tests) lacks the variable.

Source

Thrown at framework/base/Request.php:65

     * @param bool $value the value indicating whether the current request is made via command line
     */
    public function setIsConsoleRequest($value)
    {
        $this->_isConsoleRequest = $value;
    }

    /**
     * Returns entry script file path.
     * @return string entry script file path (processed w/ realpath())
     * @throws InvalidConfigException if the entry script file path cannot be determined automatically.
     */
    public function getScriptFile()
    {
        if ($this->_scriptFile === null) {
            if (isset($_SERVER['SCRIPT_FILENAME'])) {
                $this->setScriptFile($_SERVER['SCRIPT_FILENAME']);
            } else {
                throw new InvalidConfigException('Unable to determine the entry script file path.');
            }
        }

        return $this->_scriptFile;
    }

    /**
     * Sets the entry script file path.
     * The entry script file path can normally be determined based on the `SCRIPT_FILENAME` SERVER variable.
     * However, for some server configurations, this may not be correct or feasible.
     * This setter is provided so that the entry script file path can be manually specified.
     * @param string $value the entry script file path. This can be either a file path or a [path alias](guide:concept-aliases).
     * @throws InvalidConfigException if the provided entry script file path is invalid.
     */
    public function setScriptFile($value)
    {
        $scriptFile = realpath(Yii::getAlias($value));
        if ($scriptFile !== false && is_file($scriptFile)) {

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Set the value explicitly before use: Yii::$app->request->setScriptFile('@app/web/index.php')
  2. Populate $_SERVER['SCRIPT_FILENAME'] in the worker bootstrap to match the real web entry script
  3. Bootstrap CLI contexts with the console application config (yii\console\Request) instead of the web request component
  4. Where only URLs are needed, configure UrlManager's baseUrl/hostInfo explicitly instead of relying on request autodetection

Example fix

// before (queue worker reusing web config)
$file = Yii::$app->request->getScriptFile(); // InvalidConfigException: SCRIPT_FILENAME unset

// after
Yii::$app->request->setScriptFile(Yii::getAlias('@app/web/index.php'));
$file = Yii::$app->request->getScriptFile();
Defensive patterns

Strategy: fallback

Validate before calling

if (!isset($_SERVER['SCRIPT_FILENAME'])) {
    Yii::$app->request->setScriptFile(Yii::getAlias('@app/web/index.php'));
}
$file = Yii::$app->request->getScriptFile();

Try / catch

try {
    $file = Yii::$app->request->getScriptFile();
} catch (\yii\base\InvalidConfigException $e) {
    $file = Yii::getAlias('@app/web/index.php');
    Yii::$app->request->setScriptFile($file);
}

Prevention

When it happens

Trigger: A console command or queue job bootstrapped from the web config calls UrlManager/asset helpers that reach getScriptFile(); unit tests do new \yii\web\Request() directly; a non-standard SAPI or sanitized $_SERVER strips SCRIPT_FILENAME; long-running servers (Swoole/ReactPHP) reuse the component across synthetic requests.

Common situations: Queue jobs generating absolute URLs or asset paths for emails; cron tasks reusing the web application config; test harnesses instantiating the component; deployments behind custom routers that repopulate $_SERVER incompletely.

Related errors


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