在laravel 5.6中接管dingo/api 错误

有时候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);
        });

    }

效果:

猜你喜欢

转载自www.cnblogs.com/fogwang/p/13171885.html