本篇文章給大家分享的是有關如何在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接管操作,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創新互聯行業資訊頻道。
網站名稱:如何在laravel中實現Dingo-api接管操作-創新互聯
本文URL:http://www.yijiale78.com/article46/djgjhg.html
成都網站建設公司_創新互聯,為您提供網站制作、網站策劃、服務器托管、企業網站制作、電子商務、App設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯