重写 request 验证 422 错误

重写 request 验证 422 错误

我们写接口可能会 有状态码错误的冲突显示问题

php artisan make:request xxx  默认返回 422 的验证错误状态码

下面我们在request 基类中添加方法

 // 重写 reques 422 状态码
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

    public function failedValidation(Validator $validator)
    {
        throw (new HttpResponseException(response()->json([
            'status' => "error",
            'code' => 400,
            'message' => $validator->errors()->first(),
        ], 200)));
    }
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!