laravel接管Dingo-api和默認的錯誤處理方式
接管Dingo-api的錯誤

如上圖所示,AppServiceProvider.php中的register()方法中添加如下代碼
\API::error(function (\Illuminate\Validation\ValidationException $exception){
$data =$exception->validator->getMessageBag();
$msg = collect($data)->first();
if(is_array($msg)){
$msg = $msg[0];
}
return response()->json(['message'=>$msg,'status_code'=>400], 200);
});
\API::error(function (\Dingo\Api\Exception\ValidationHttpException $exception){
$errors = $exception->getErrors();
return response()->json(['message'=>$errors->first(),'status_code'=>400], 200);
});
接管laravel的錯誤

在Exceptions的Handler.php的render中寫入以下代碼
public function render($request, Exception $exception)
{
if($exception instanceof \Illuminate\Validation\ValidationException){
$data = $exception->validator->getMessageBag();
$msg = collect($data)->first();
if(is_array($msg)){
$msg = $msg[0];
}
return response()->json(['message'=>$msg],200);
}
if (in_array('api',$exception->guards())){
if($exception instanceof AuthenticationException){
return response()->json(['message'=>'token錯誤'],200);
}
if($exception instanceof ModelNotFoundException){
return response()->json(['message'=>'該模型未找到'],200);
}
}
return parent::render($request, $exception);
}
以上這篇laravel接管Dingo-api和默認的錯誤處理方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
ubutu 16.04環(huán)境下,PHP與mysql數(shù)據(jù)庫,網(wǎng)頁登錄驗證實例講解
下面小編就為大家?guī)硪黄猽butu 16.04環(huán)境下,PHP與mysql數(shù)據(jù)庫,網(wǎng)頁登錄驗證實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
Codeigniter上傳圖片出現(xiàn)“You did not select a file to upload”錯誤解決辦法
這篇文章主要介紹了Codeigniter上傳圖片出現(xiàn)“You did not select a file to upload”的解決辦法,需要的朋友可以參考下2014-06-06
全新的PDO數(shù)據(jù)庫操作類php版(僅適用Mysql)
在公司里也用了1年之久。如今公司規(guī)模變大了,產(chǎn)品也日益完善,曾經(jīng)的那個數(shù)據(jù)庫操作函數(shù)雖說使用上沒出什么大問題,但為了更顯專業(yè),花了1天時間重寫了這個,現(xiàn)在,它確實是個類了2012-07-07
淺析PHP中call user func()函數(shù)及如何使用call user func調(diào)用自定義函數(shù)
使用call_user_func函數(shù),通過傳入字符串函數(shù),可以調(diào)用自定義函數(shù),并且支持引用。該函數(shù)允許用戶調(diào)用直接寫的函數(shù)并傳入一定的參數(shù),下面總結下這個函數(shù)的使用方法,需要的朋友參考下2015-11-11

