How to detect if I am in 'console' mode
By default for console:
Yii::$app->id == 'basic-console'
And for web application:
Yii::$app->id == 'basic'
Yii::$app->id
stores the id of the loaded configuration params. By default for console application it is 'basic-console'
and for web application it is 'basic'
(defined in configuration file)
Correct variant
Yii::$app->request->isConsoleRequest
You can use
if (Yii::$app instanceof \yii\console\Application)
for console, and
if (Yii::$app instanceof \yii\web\Application)
for web.
There is a simpler way to figure this out without going through the Yii
objects
if (php_sapi_name() == "cli") {
return;
}
...and it works for all PHP scripts ...and it is lighter