Laravel 403 forbidden on custom Request validation

こ雲淡風輕ζ 提交于 2021-02-20 19:01:46

问题


I've followed the docs here:

https://laravel.com/docs/5.8/validation#form-request-validation

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();
}

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).

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.

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.

What could the problem be?


EDIT:

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;
}

回答1:


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
}



回答2:


public function authorize()
{
    return false;
}

on your requests make change false to true

    public function authorize()
{
    return true;
}


来源:https://stackoverflow.com/questions/56235420/laravel-403-forbidden-on-custom-request-validation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!