gpt4 book ai didi

php - 请求验证总是使用 Dingo/Api 在 Laravel 上通过

转载 作者:搜寻专家 更新时间:2023-10-31 21:26:43 25 4
gpt4 key购买 nike

我正在使用 dingo/api包。

Controller :

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,但还是一样。

最佳答案

为了让 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: This is tested on Laravel 5.2.*

希望对您有所帮助:)

关于php - 请求验证总是使用 Dingo/Api 在 Laravel 上通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35044613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com