如何使用代码在laravel控制台命令中中止/退出/停止/终止

kej*_*ion 2 php command laravel laravel-5

假设我正在编写命令。如何在运行中完全停止运行?

例:

public function handle()
{
    if (!$this->good_times) {
        $this->error('Bad times');
        $this->exit();
    }

    // continue command stuff
}
Run Code Online (Sandbox Code Playgroud)

我试过了:

throw new RuntimeException('Bad times');
Run Code Online (Sandbox Code Playgroud)

但这会在终端中丢掉一堆丑陋的东西。

Yah*_*din 6

也遇到了这个问题。

只需创建一个实现ExceptionInterface.

use Exception;
use Symfony\Component\Console\Exception\ExceptionInterface;

class ConsoleException extends Exception implements ExceptionInterface
{

}
Run Code Online (Sandbox Code Playgroud)

现在当你抛出错误时:

throw new ConsoleException('Bad times');
Run Code Online (Sandbox Code Playgroud)

您会在没有堆栈跟踪的情况下收到错误。


bma*_*ovu 5

只需使用一条return语句而不是引发异常。喜欢...

public function handle()
{
    if (!$this->good_times) {
        $this->error('Bad times');
        // $this->exit();
        // throw new RuntimeException('Bad times');
        return;
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)