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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
tp5 實現(xiàn)列表數(shù)據(jù)根據(jù)狀態(tài)排序
今天小編就為大家分享一篇tp5 實現(xiàn)列表數(shù)據(jù)根據(jù)狀態(tài)排序,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP+jQuery+Ajax實現(xiàn)分頁效果 jPaginate插件的應(yīng)用
這篇文章主要介紹了PHP+jQuery+Ajax實現(xiàn)分頁效果,以及jPaginate插件的應(yīng)用2015-10-10
Yii模型操作之criteria查找數(shù)據(jù)庫的方法
這篇文章主要介紹了Yii模型操作之criteria查找數(shù)據(jù)庫的方法,結(jié)合實例形式分析了Yii模型中criteria的實例化與查詢操作相關(guān)技巧,需要的朋友可以參考下2016-07-07
thinkphp隱藏index.php/home并允許訪問其他模塊的實現(xiàn)方法
這篇文章主要介紹了thinkphp隱藏index.php/home并允許訪問其他模塊的實現(xiàn)方法,想要達成的效果很簡單,我有兩個模塊,Home、Wechat。具體詳情請參考下本文。感興趣的朋友一起看看吧2016-10-10
修改Laravel自帶的認證系統(tǒng)的User類的命名空間的步驟
今天小編就為大家分享一篇修改Laravel自帶的認證系統(tǒng)的User類的命名空間的步驟,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP利用正則表達式實現(xiàn)手機號碼中間4位用星號(*)替換顯示功能
為了我們的隱私,所以我們把手機號碼部分數(shù)字隱藏掉,今天小編給大家?guī)砹薖HP利用正則表達式實現(xiàn)手機號碼中間4位用星號(*)替換顯示,需要的朋友參考下吧2017-10-10
smarty自定義函數(shù)htmlcheckboxes用法實例
這篇文章主要介紹了smarty自定義函數(shù)htmlcheckboxes用法,實例分析了smarty模板與函數(shù)的使用技巧,需要的朋友可以參考下2015-01-01

