3

我正在使用dingo/api包。

控制器:

public function register(RegisterUserRequest $request)
{
    dd('a');
}

例如,电子邮件字段是必需的:

<?php namespace App\Http\Requests;


class RegisterUserRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'email' => 'required'
        ];
    }
}

所以我在没有电子邮件的情况下发送了一个请求,但仍然得到“a”响应。

我也尝试扩展Dingo\Api\Http\Request而不是App\Http\Request,但仍然相同。

4

3 回答 3

1

要让 Dingo 完全使用FormRequest,根据经验(以及从这个 Issue),您必须使用Dingo 的Form request ie Dingo\Api\Http\FormRequest;,因此您将拥有类似于以下内容的内容:

<?
namespace App\Http\Requests;
use Dingo\Api\Http\FormRequest;
use Symfony\Component\HttpKernel\Exception\HttpException;


class RegisterUserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
     public function rules()
     {
        return [
            'email' => 'required'
        ];
     }
    // In case you need to customize the authorization response
    // although it should give a general '403 Forbidden' error message
    /**
     * Handle a failed authorization attempt.
     *
     * @return mixed
     */
     protected function failedAuthorization()
     {
         if ($this->container['request'] instanceof \Dingo\Api\Http\Request) {
            throw new HttpException(403, 'You cannot access this resource'); //not a user?
         }

     }
}

PS:这是在 Laravel 5.2 上测试的。*

希望能帮助到你 :)

于 2017-01-26T11:29:39.733 回答
0

根据维基

您必须重载 failedValidation 和 failedAuthorization 方法。这些方法必须抛出上述异常之一,而不是 Laravel 抛出的响应 HTTP 异常。

如果你看一下 Dingo\Api\Http\FormRequest.php,你会看到:

class FormRequest extends IlluminateFormRequest
{
    /**
     * Handle a failed validation attempt.
     *
     * @param \Illuminate\Contracts\Validation\Validator $validator
     *
     * @return mixed
     */
    protected function failedValidation(Validator $validator)
    {
        if ($this->container['request'] instanceof Request) {
            throw new ValidationHttpException($validator->errors());
        }

        parent::failedValidation($validator);
    }

    /**
     * Handle a failed authorization attempt.
     *
     * @return mixed
     */
    protected function failedAuthorization()
    {
        if ($this->container['request'] instanceof Request) {
            throw new HttpException(403);
        }

        parent::failedAuthorization();
    }
}

因此,您需要适当地更改方法的名称,并让它们抛出适当的异常,而不是返回布尔值。

于 2016-03-22T22:58:43.450 回答
0

当您在 Dingo API 设置下运行验证函数时,您需要显式调用验证函数,尝试这样的操作(对于 L5.2):

可能有一些额外的供应商

...
Illuminate\Validation\ValidationServiceProvider::class,
Dingo\Api\Provider\LaravelServiceProvider::class,
...

别名

...
'Validator' => Illuminate\Support\Facades\Validator::class,
...

我也很确定你真的不想按照这里和那里的建议在下面使用它,它会期望表单(编码)输入,并且也可能会像它所期望的那样在 CSRF 令牌上失败,所以它会正确失败验证后(表单输入)。但请确保使用此开/关测试行为。

use Dingo\Api\Http\FormRequest;

制作标题:

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Dingo\Api\Exception\ValidationHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/* This can be a tricky one, if you haven't split up your 
 dingo api from the http endpoint, there are plenty 
 of validators around in laravel package 
*/

use Validator; 

然后是实际代码(如果您遵守 cors 标准,这应该是一个 POST 并且通常转换为存储请求)

...
/**
* Store a newly created resource in storage.
*
* @param  \Illuminate\Http\Request  $request
* @return \Illuminate\Http\Response
*/
public function register(RegisterUserRequest $request) {
    $validator = Validator::make($request->all(), $this->rules());
    if ($validator->fails()) {
        $reply = $validator->messages();
        return response()->json($reply,428);
    };
    dd('OK!');
};
...

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
    return [
            'email'   => 'required'
            // or/and 'userid'     => 'required'
    ];
}

这将使您返回您期望从验证器获得的响应。如果您将其与预生成的表单一起使用,则不需要此修复,验证器将自动启动。(不在 Dingo Api 下)。

您可能还需要 composer.json 中的这些

    "dingo/api": "1.0.*@dev",
    "barryvdh/laravel-cors": "^0.7.1",

这是未经测试的,我花了 2 天时间才弄清楚这一点,但我有一个单独的命名空间用于特定于 API 并通过中间件进行身份验证。成功

于 2017-02-28T18:04:37.640 回答