How to detect if I am in ‘console’ mode


I am writing an app that runs from the browser. However, some model functions are also called from the Yii2 console. Therefore, I am getting errors when trying to access variables that are set in the GUI.

Is it possible to tell which mode I am in? Is there some environment variable automatically set, or should I just set some session variable in the console app to indicate the state?

Best Solution

You can use

if (Yii::$app instanceof \yii\console\Application)

for console, and

if (Yii::$app instanceof \yii\web\Application)

for web.

Related Question