gpt4 book ai didi

Laravel Fortify 不响应 HTTP 代码而是响应实际路由,即使我将注册请求作为 XHR 发送

转载 作者:行者123 更新时间:2023-12-05 00:54:19 25 4
gpt4 key购买 nike

上下文

使用 Postman,为了在数据库中注册用户,我将以下字段发送到 URL http://mywebsite/register:

  • 电子邮件

  • 密码

  • 密码确认

  • 名字

根据文档 https://laravel.com/docs/8.x/fortify#registration (从中我找到了上述字段),Fortify 已经定义了路由 register 所以我不需要自己定义它。

根据文档和我的需要,我不需要创建注册表单:我直接使用 Postman 将这些注册数据发送到 Laravel Fortify 的路由 /register 作为 XHR POST 请求数据。此外,我不需要 Fortify 来返回 View ,所以我禁用了它们 (https://laravel.com/docs/8.x/fortify#disabling-views)。事实上,我只是等待我将在 Postman 的返回数据中看到的 HTTP 代码响应(见下文)。

Fortify 应该返回什么

由于我禁用了 View 并且因为我发送了 XHR POST 请求(在 Postman 中,我将这个 HTTP header 与注册请求一起发送:X-Requested-With = XMLHttpRequest):

If the registration attempt is successful, Fortify will redirect the user to the URI configured via the home configuration option within your application's fortify configuration file. If the login request was an XHR request, a 200 HTTP response will be returned.

If the request was not successful, the user will be redirected back to the registration screen and the validation errors will be available to you via the shared $errors Blade template variable. Or, in the case of an XHR request, the validation errors will be returned with a 422 HTTP response.

( https://laravel.com/docs/8.x/fortify#registration )

Fortify 实际返回的内容

它正确地注册了用户。但是我在 Postman 中看到的结果是 Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException。事实上,Fortify 正在尝试访问主视图。

问题

为什么 Fortify 似乎仍在尝试访问返回的 View 路由(未定义,因为我不需要它们),因为我的请求正确地是 XHR POST 请求并且我已经禁用了 Fortify 配置文件中的 View ?

最佳答案

我想我在 Fortify 文档 (https://laravel.com/docs/8.x/fortify#registration) 中发现了一些“有趣”的东西。那里有些东西是不完整的。我想我已经找到了文档中缺少的内容!

我解释一下。

在我的例子中,一个手机应用会显示一个注册表单。它发送 Fortify 的注册必填字段,如 email, password, confirmation_password, ...到 URL <LaravelSite>/register这是由 Fortify 定义的。Fortify 成功注册用户并将他重定向到主路由。但是,我没有定义任何主路由,因为正如您所理解的,这个 Laravel 站点是一个 API。结果 => 我有这个错误:Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException .

我已关注文档:我已禁用 View (在 Fortify 配置文件中)。我已正确指定电话应用程序(实际上是 Postman)发送的请求是 XHR 请求(实际上,我已发送 header X -Requested-With = XMLHttpRequest )。所以 Fortify 应该返回给我一个 HTTP 代码(此处为 200),而不是尝试返回主页 URL。

文档中缺少什么? =====>>>>中间件RedirectIfAuthenticated被执行。在它的句柄方法中有一个重定向:return redirect(RouteServiceProvider::HOME); .为了使 Fortify 正常工作,必须通过注释禁用此行。

Fortify 文档应该包含与上述句子类似的内容。

应在其 Github 存储库中创建 PR 或问题。

编辑:我们还必须发送这个 HTTP header :Accept: application/json (否则注册成功还是会显示404)

关于Laravel Fortify 不响应 HTTP 代码而是响应实际路由,即使我将注册请求作为 XHR 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66563051/

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