Php 使用Dingo/Api在Laravel上始终传递请求验证
我用的是软件包 控制器:Php 使用Dingo/Api在Laravel上始终传递请求验证,php,laravel,laravel-5.1,dingo-api,Php,Laravel,Laravel 5.1,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 r
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'
];
}
}
根据
必须重载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();
}
}
因此,您需要适当地更改方法的名称,并让它们抛出适当的异常,而不是返回布尔值。要让Dingo使用FormRequest,根据经验(以及从),您必须使用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?
}
}
}
在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标准,
这应该是一篇文章,通常会转化为一个商店请求)
这将返回您期望从验证器得到的响应。如果您将其用于预生成的表单,则不需要此修复,验证程序将自动启动。(不适用于澳洲野狗Api)
在composer.json中可能还需要这些
"dingo/api": "1.0.*@dev",
"barryvdh/laravel-cors": "^0.7.1",
这是未经测试的,从本质上讲,我花了2天的时间才弄明白这一点,但我有一个单独的名称空间用于特定于API的,并通过中间件进行身份验证。成功
"dingo/api": "1.0.*@dev",
"barryvdh/laravel-cors": "^0.7.1",