Verify if console aplication or web aplication

How verify if method on model was called by web application or the console application.

The following method does not work when called by the console:

$ Log-> ip = \ Yii :: $ app-> request-> userIP;

$ Log-> usuario_id = \ Yii :: $ app-> user-> id;

Indeed, if we run the console does not exist a user and an IP.

soon, I would have a checker for this method in my model to find out if it’s a web request or console request

ex: if (yii resquest is web app) {

$ Log-> ip = \ Yii :: $ app-> request-> userIP; $ Log-> usuario_id = \ Yii :: $ app-> user-> id;

}

Why not set a parameter in console\config\params.php and check for the parameter using \Yii::$app->params[‘value’]?

I don’t know if there is a built-in way to do it.

I guess you can check


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

or


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