Search code examples
phplaravellaravel-5command

how to Abort/exit/stop/terminate in laravel console command using code


Let's say I'm coding a command. How would I stop it completely in the middle of it running?

Example:

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

    // continue command stuff
}

I have tried:

throw new RuntimeException('Bad times');

But that dumps a bunch of ugliness in the terminal.


Solution

  • Just use a return statement instead of throwing an exception. Like...

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