How can a Laravel 4 class detect that it is running in an Artisan task vs. a browser request?

Deadly 提交于 2019-12-08 01:18:02

问题


I have some application boot code that needs to know if it is currently running in an artisan task vs. being called in a browser request.

How do I detect this in Laravel 4?


回答1:


Here is the best way to do it :)

    if (App::runningInConsole())
      echo "Running in artisan/CLI";
    else 
      echo "Running in a browser";

Hope it helps.




回答2:


runningInConsole() also returns true when unit tests are running, then I think a better way to detect if your app is running only through artisan is:

if (App::runningInConsole() && !App::runningUnitTests()) {
    // ... code to execute when artisan is running.
}


来源:https://stackoverflow.com/questions/24697363/how-can-a-laravel-4-class-detect-that-it-is-running-in-an-artisan-task-vs-a-bro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!