laravel接管Dingo-api和默认的错误处理方式
更新时间:2019年10月25日 09:18:30 作者:zhangatle
今天小编就为大家分享一篇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和默认的错误处理方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
ubutu 16.04环境下,PHP与mysql数据库,网页登录验证实例讲解
下面小编就为大家带来一篇ubutu 16.04环境下,PHP与mysql数据库,网页登录验证实例讲解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-07-07
Codeigniter上传图片出现“You did not select a file to upload”错误解决办法
这篇文章主要介绍了Codeigniter上传图片出现“You did not select a file to upload”的解决办法,需要的朋友可以参考下2014-06-06
浅析PHP中call user func()函数及如何使用call user func调用自定义函数
使用call_user_func函数,通过传入字符串函数,可以调用自定义函数,并且支持引用。该函数允许用户调用直接写的函数并传入一定的参数,下面总结下这个函数的使用方法,需要的朋友参考下2015-11-11


最新评论