如何检测我是否处于“控制台"模式 [英] How to detect if I am in 'console' mode

查看:23
本文介绍了如何检测我是否处于“控制台"模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个从浏览器运行的应用程序.然而,一些模型函数也是从 Yii2 控制台调用的.因此,我在尝试访问 GUI 中设置的变量时遇到错误.

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?

推荐答案

可以使用

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

用于控制台,和

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

用于网络.

这篇关于如何检测我是否处于“控制台"模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆