使用Lumen創建一個API--愛Laravel,但所有與View一起的View對於我創建的項目來說都是過度的。如何巧妙地處理Artisan命令中的例外
無論如何,我已經做出了一系列命令,它們出去收集數據並將其存儲到數據庫中。
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use App\User;
class GetItems extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'GetItems';
/**
* The console command description.
*
* @var string
*/
protected $description = "Get items and store it into the Database";
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$this->info("Collecting ...");
$users = User::all();
foreach($users as $user)
{
$user->getItems();
}
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [];
}
}
我有3個類似的命令,每一個都收集稍微不同的數據集。
有沒有一種方法可以注入一箇中間層,捕獲來自我的命令中每個fire()
函數的異常?我正在考慮擴展Command
類 - 但是想看看框架創建者是否已經有一種方法可以實現推薦(文檔/搜索沒有幫助)。
我知道替代方法是將所有命令組合成一個文件並使用選項,但是這使得它很難與之協作。
有什麼建議嗎?
你嘗試使用'嘗試{}趕上(\例外$ E){}'內' - > fire()'方法?你得到的錯誤是什麼?那個異常來自哪裏? – ljubadr
@ljubadr是的,只能在該類中工作,但因爲我有許多類似的類,所以我必須重複try {} catch塊。我覺得將一個異常向上堆棧並將其捕獲到堆棧中'fire()'被調用 – Moe