在哪个位置重写`failedValidation`和`failedAuthorization`方法?

如题, \Dingo\Api\Exception\ValidationHttpException类中并没有对应的方法, 要怎样重写呢.
主要目的是表单验证失败时能返回自定义字段格式的内容.
请大佬指点指点

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
最佳答案

继承Dingo API的FormRequet的Request中
如:

<?php

namespace App\Http\Requests;

use Dingo\Api\Exception\ResourceException;
use Illuminate\Contracts\Validation\Validator;
use Dingo\Api\Http\FormRequest;
use Illuminate\Validation\ValidationException;

class Request extends FormRequest
{
    /**
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * @param Validator $validator
     */
    protected function failedValidation(Validator $validator)
    {
        if ($this->container['request'] instanceof \Illuminate\Http\Request) {
            throw new ResourceException($validator->errors()->first(), null);
        }

        throw (new ValidationException($validator))
            ->errorBag($this->errorBag)
            ->redirectTo($this->getRedirectUrl());
    }
}

显示效果如下:

{
    "status_code": 422,
    "message": "图片验证码 key 不能为空"
}
4年前 评论
讨论数量: 6

继承Dingo API的FormRequet的Request中
如:

<?php

namespace App\Http\Requests;

use Dingo\Api\Exception\ResourceException;
use Illuminate\Contracts\Validation\Validator;
use Dingo\Api\Http\FormRequest;
use Illuminate\Validation\ValidationException;

class Request extends FormRequest
{
    /**
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * @param Validator $validator
     */
    protected function failedValidation(Validator $validator)
    {
        if ($this->container['request'] instanceof \Illuminate\Http\Request) {
            throw new ResourceException($validator->errors()->first(), null);
        }

        throw (new ValidationException($validator))
            ->errorBag($this->errorBag)
            ->redirectTo($this->getRedirectUrl());
    }
}

显示效果如下:

{
    "status_code": 422,
    "message": "图片验证码 key 不能为空"
}
4年前 评论

@mingzaily 谢谢, 真的对我很有帮助~ :smile:

4年前 评论

@Laland 我现在都是这么写的了

    /**
     * @param Validator $validator
     */
    protected function failedValidation(Validator $validator)
    {
        throw new ValidateException($validator->errors()->first(),422);
    }

然后在App\Exceptions\Handler的render进行捕获

4年前 评论

@mingzaily 受到你的启发, 我的做法是抛出一个继承自Symfony\Component\HttpKernel\Exception\HttpException的异常CommonException, 然后再由App/Providers/AppServiceProvider::register方法捕获并处理, 最终返回自定义的数据格式.

    protected function failedValidation(Validator $validator)
    {
            throw new CommonException($validator->errors()->first());
    }

App/Providers/AppServiceProvider

    public function register()
    {
        app(\Dingo\Api\Exception\Handler::class)->register(function (\App\Exceptions\Api\CommonException $exception) {
            return response($exception->getMsg(), $exception->getStatusCode());
        });
    }

其中getMsg()是写在CommonException里的拼接数据的自定义方法, 最终返回的数据格式类似:

{
    "data": [],
    "meta": {
        "code": 1100,
        "msg": "The code field is required."
    }
}

目的是表单检验通过与否都返回相同的数据格式.

之所以在服务提供者里捕获是因为我这边的App\Exceptions\Handler ::render(类继承的是Illuminate\Foundation\Exceptions\Handler)根本不会被调用, 不管的是抛出什么异常(laravel5.8), 能说说你是怎么做到的吗

4年前 评论

@Laland

  • 你这样写应该是注册以后把所有的dingo\api错误信息,由你写的CommonException来进行处理?这个Exception是继承Laravel本身的Exception是这样理解没错吧?
  • 你需要把 App\Exceptions\Handler 改成继承 Dingo\API 的Exception的handler 才能直接在render中捕获(原先是继承use Illuminate\Foundation\Exceptions\Handler)
  • 我在后面继续开发过程中,就不使用`dingo\api'了,我感觉Laravel本身的已经足够了
  • 这是我的仓库地址 https://git.dev.tencent.com/mingzaily/irea... 现在开放了,供你参考,希望有所帮助
  • 也可以添加我的扣扣 1057106591 一起讨论,我也是刚开始学习
4年前 评论

@mingzaily CommonException的作用正如你所理解的那样没错.

其实我也是尝试过把App\Exceptions\Handler改成继承于Dingo\Api\Exception\Handler的, 可结果是整个程序无法正常运行, 表现为所有请求都无内容返回, 网络状态码是500, 连日志文件也没有错误记录. 所以这个方案还是放一放吧.
很感谢你的分享哦~

4年前 评论

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