问题
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