Laravel 403禁止自定义请求验证 [英] Laravel 403 forbidden on custom Request validation
问题描述
我在这里关注了文档:
https://laravel.com/docs/5.8/validation#form-请求验证
我创建了一个自定义请求StoreName
I created a custom request StoreName
php artisan make:request StoreName
然后添加了以下验证规则:
Then added the following validation rules:
public function rules()
{
return [
'name' => 'required|max:255|min:4'
];
}
然后根据文档在我的控制器中键入的提示:
Then as per the documentation type-hinted this in my controller:
public function store(StoreName $request)
{
$validated = $request->validated();
}
但是,当我向该端点发送发布请求时,我从Laravel返回了 403
.当我删除StoreName自定义验证并只是简单地键入标准Laravel Illuminate \ Http \ Request
时,请求就可以正常工作(显然是在删除validated()方法之后).
However, when I send a post request to this endpoint I'm returned a 403
from Laravel. When I remove the StoreName custom validation and simply type-hint the standard Laravel Illuminate\Http\Request
the request works fine (after removing the validated() method obviously).
因此403来自我的自定义验证请求,我不知道为什么吗?我已经检查了StoreName.php文件的文件权限,它们与项目中的所有其他文件相同.
So the 403 is coming from my custom validation request and I have no idea why? I've checked the file permissions on the StoreName.php file and they are the same as every other file in the project.
我正在为我的开发服务器使用 php artisan serve
,因此也没有时髦的Apache/Nginx配置覆盖任何东西.应用自定义验证请求时,所有其他端点均会与此端点分开工作.
I'm using php artisan serve
for my development server so no funky Apache/Nginx configs overwriting things either. All other endpoints work apart from this one when the custom validation request is applied.
可能是什么问题?
值得注意的是,我也没有更改Laravel在新的自定义请求验证中生成的默认authorize()方法:
It's probably worth noting I've not changed the default authorize() method Laravel generates in the new custom request validation either:
public function authorize()
{
return false;
}
推荐答案
在您的自定义"请求类中,您有一个像这样的方法:
In your Custom request class you've got a method like this:
public function authorize()
{
return true; // this is false by default which means unauthorized 403
}
这篇关于Laravel 403禁止自定义请求验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!