请求验证总是使用Dingo / Api传递Laravel

时间:2016-01-27 17:52:33

标签: php laravel laravel-5.1 dingo-api

我正在使用dingo/api包。

控制器:

WITH 
  MEMBER [MyCount] AS 
    Count([Date].[Date].MEMBERS) 
SELECT 
  {[MyCount]} ON 0
FROM 
(
  SELECT 
    {[Measures].[Sales Amount]} ON 0
   ,{[Date].[Date].&[20050701] : [Date].[Date].&[20051231]} ON 1
  FROM 
  (
    SELECT 
      {[Sales Channel].[Sales Channel].&[Internet]} ON 0
    FROM [Adventure Works]
  )
);

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

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

所以我在没有电子邮件的情况下发送请求,仍然得到“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' ]; } } 而不是Dingo\Api\Http\Request,但仍然相同。

3 个答案:

答案 0 :(得分:1)

要使Dingo完全使用 FormRequest ,根据经验(以及来自this Issue),您必须使用 Dingo的表单请求即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上测试的。*

希望有所帮助:)

答案 1 :(得分:0)

根据Wiki

  

您必须重载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();
    }
}

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

答案 2 :(得分:0)

你需要在Dingo API设置下运行它时显式调用validate函数,尝试这样的事情(对于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",

这是未经测试的,但是我花了两天的时间才弄明白这一点,但我有一个独立的API命名空间,并通过中间件进行身份验证。成功