zl程序教程

您现在的位置是:首页 >  其他

当前栏目

在laravel 5.6中接管dingo/api 错误

错误API laravel 5.6 接管
2023-09-11 14:19:35 时间

有时候dingo/api返回的错误信息并不是我们需要的格式,我们需要自定义

在app\Providers\AppServiceProvider类register加入以下方法即可,使用

use Dingo\Api\Facade\API;

  public function 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(['data'=>$msg,'code'=>1], 200);
        });

    }

效果: