Laravel 任意将退出状态代码设置为自定义命令
Laravel arbitary set exit status code to custom command
我正在为 运行 后台进程执行以下自定义命令:
namespace App\Console\Commands;
use App\Model\MyModel;
use Exception;
use Illuminate\Console\Command;
class MyCommand extends Command
{
/**
* @var string
*/
protected $description = "Doing StuffyStuff";
/**
* @var string
*/
protected $signature = "mycommand:dostuff";
public function __construct()
{
parent::__construct();
}
public function handle(MyModel $model): void
{
//@todo Implement Upload
try {
$model->findOrFail(12);
//Set Exit Status Code 0
} catch (Exception $e) {
//Set status code 1
}
}
}
如您所见,我想根据是否抛出异常来指定状态代码。如果成功,我想使用退出状态代码 0,如果失败,我想使用 Unix 规范指定的退出状态代码 1。
那么你知道怎么做吗?
您可以return代码在任何您想要的地方
public function handle(MyModel $model): int
{
//@todo Implement Upload
try {
$model->findOrFail(12);
return 0;
} catch (Exception $e) {
$this->error($e->getMessage());
return 1;
}
}
你可以看个例子解释一下here
我正在为 运行 后台进程执行以下自定义命令:
namespace App\Console\Commands;
use App\Model\MyModel;
use Exception;
use Illuminate\Console\Command;
class MyCommand extends Command
{
/**
* @var string
*/
protected $description = "Doing StuffyStuff";
/**
* @var string
*/
protected $signature = "mycommand:dostuff";
public function __construct()
{
parent::__construct();
}
public function handle(MyModel $model): void
{
//@todo Implement Upload
try {
$model->findOrFail(12);
//Set Exit Status Code 0
} catch (Exception $e) {
//Set status code 1
}
}
}
如您所见,我想根据是否抛出异常来指定状态代码。如果成功,我想使用退出状态代码 0,如果失败,我想使用 Unix 规范指定的退出状态代码 1。
那么你知道怎么做吗?
您可以return代码在任何您想要的地方
public function handle(MyModel $model): int
{
//@todo Implement Upload
try {
$model->findOrFail(12);
return 0;
} catch (Exception $e) {
$this->error($e->getMessage());
return 1;
}
}
你可以看个例子解释一下here