今天小编就为大家分享一篇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和默认的错误处理方式
猜你喜欢
- PHP中PDO事务处理操作示例 2022-10-15
- Laravel balde模板文件中判断数据为空方法 2023-08-30
- PHP实现微信支付(jsapi支付)流程步骤详解 2022-10-09
- windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境) 2023-09-02
- laravel实现按月或天或小时统计mysql数据的方法 2023-02-22
- PHP简单实现二维数组的矩阵转置操作示例 2022-10-02
- 用nohup命令实现PHP的多进程 2023-09-02
- php微信公众号开发之秒杀 2022-11-23
- laravel通用化的CURD的实现 2023-03-17
- PHP仿tp实现mvc框架基本设计思路与实现方法分析 2022-10-18