【问题标题】:Laravel 8 Illuminate\Foundation\Http\FormRequest with Ajax Error 403 ForbiddenLaravel 8 Illuminate\Foundation\Http\FormRequest 与 Ajax 错误 403 禁止
【发布时间】:2021-09-21 20:48:57
【问题描述】:

我正在重新做一个网站的一部分,并决定使用 Laravel 8 及其内置的存储库模式。我建立了项目,首先在一个处理所有首页方法的 WelcomeController 中完成了所有工作。一切都很好。

然后,我抽象了 WelcomeController 方法的部分功能
1> 表单验证 => App\Http\Requests\WelcomeRequest
2> 使用的方法 => App\Interfaces\WelcomeInterface
3> 实现的方法 => App\Repositories\WelcomeRepository
4> 注入接口 => App\Http\Controllers\WelcomeController
5> 绑定接口 + 存储库 => Providers\RepositoryWebServiceProvider
6> 注册服务商RepositoryWebServiceProvider => config/app.php
7> 命令 => php artisan config:cache & composer dump-autoload

首页已加载。欢呼!

然后,我使用 ajax 发布了联系表单值,并收到了 403 Forbidden http 响应代码。

A] 为了尝试解决这个问题,我将 WelcomeController store 方法中的 WelcomeRequest 参数替换为 Illuminate\Http\Request 的实例,并将请求存入 WelcomeController store 方法。
B] 但是在注入接口方法$this->welcomeInterface->contactEmail($request) 上再次出错,错误消息expected WelcomeRequest $request instance Illuminate\Http\Request given
C] 403 禁止错误仅在实例化 App\Http\Requests\WelcomeRequest 类时发生。

App\Http\Controllers\WelcomeController

use App\Http\Requests\WelcomeRequest;
use App\Interfaces\web\WelcomeInterface;

class WelcomeController extends Controller
{
    protected $welcomeInterface;

    public function __construct(WelcomeInterface $welcomeInterface)
    {
        $this->welcomeInterface = $welcomeInterface;
    }

    // ...
    public function store(WelcomeRequest $request) // the request argument causes the error
    {
        return $this->welcomeInterface->requestContact($request);
    }

Routes\web.php

Route::post('/email-contact',  [App\Http\Controllers\WelcomeController::class, 'store'])->name('email-contact');

App\Http\Requests\WelcomeRequest

class WelcomeRequest extends FormRequest
{
    public function authorize()
    {
        return false;
    }

    public function rules()
    {
        return [
            'name'=> 'required|string|max:60',
            'email'=> 'required|string|email|max:255',
            'subject' => 'required|string|max:60',
            'message' => 'nullable|string|max:300',
        ];
    }

    public function messages()
    {
        return [
            'name.required' => 'Your name is required and should not exceed maximum length of 60.',
            'name.max' => 'Your name is required and should not exceed maximum length of 60.',
            'email.required' => 'Email is required.',
            'email.email' => 'Email is incorrect format.',
            'subject.required' => 'The subject is required and should not exceed maximum length of 60.',
            'subject.max' => 'The subject is required and should not exceed maximum length of 60.',
            'message.max' => 'Comment maximum length exceeded.',
            'message.string' => 'Comment is invalid format.',
        ];
    }
}

我的问题是为什么使用扩展Illuminate\Foundation\Http\FormRequestApp\Http\Requests\WelcomeRequest 类给我一个403 Forbidden http 响应?我该如何解决?

【问题讨论】:

    标签: php xmlhttprequest laravel-8 repository-pattern http-status-code-403


    【解决方案1】:

    解决了。 WelcomeRequest 类需要在允许验证之前获得授权。因此我更改了以下方法返回--

        public function authorize()
        {
            return true;
        }
    

    【讨论】:

    • 正在写那个 ;) 很高兴你找到它!
    猜你喜欢
    • 1970-01-01
    • 2020-12-30
    • 2011-07-12
    • 2017-08-18
    • 2017-03-23
    • 1970-01-01
    • 2015-02-25
    • 2017-12-20
    • 2011-12-14
    相关资源
    最近更新 更多