【发布时间】:2021-06-09 12:45:47
【问题描述】:
我正在使用 Laravel 8 jetstream 进行身份验证。我的问题是,如何在将密码重置为自定义路由后重定向用户?我不想将用户重定向到登录页面。我没有在所有 Fortify 课程中找到路线;我确信它应该覆盖。
受保护的 $redirectTo
但我不知道我必须在哪个文件中进行此更改。
【问题讨论】:
我正在使用 Laravel 8 jetstream 进行身份验证。我的问题是,如何在将密码重置为自定义路由后重定向用户?我不想将用户重定向到登录页面。我没有在所有 Fortify 课程中找到路线;我确信它应该覆盖。
受保护的 $redirectTo
但我不知道我必须在哪个文件中进行此更改。
【问题讨论】:
编辑:不建议在vendor中编辑文件,使用BillD的解决方案。
查看vendor\laravel\fortify\src\Http\Responses\PasswordResetResponse.php
你应该可以修改方法中的响应:
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function toResponse($request)
{
return $request->wantsJson()
? new JsonResponse(['message' => trans($this->status)], 200)
: redirect()->route('login')->with('status', trans($this->status));
}
【讨论】:
在用户提交密码重置操作后,我最终将重定向回登录路径:
将文件 SuccessfulPasswordResetLinkRequestResponse.php 从 \vendor\laravel\fortify\Http\Responses\ 复制到项目中位于 app\Http\Responses 的文件夹中。
在您的新文件SuccessfulPasswordResetLinkRequestResponse.php 中,将命名空间更改为:
namespace App\Http\Responses;
打开app\Providers\FortifyServiceProvider.php
在boot()函数内添加:
public function boot()
{
...
$this->app->singleton(SuccessfulPasswordResetLinkRequestResponseContract::class, SuccessfulPasswordResetLinkRequestResponse::class);
}
FortifyServiceProvider.php 文件中,添加命名空间:use App\Http\Responses\SuccessfulPasswordResetLinkRequestResponse;
use Laravel\Fortify\Contracts\SuccessfulPasswordResetLinkRequestResponse as SuccessfulPasswordResetLinkRequestResponseContract;
SuccessfulPasswordResetLinkRequestResponse.php 文件中,编辑toResponse() 函数: public function toResponse($request)
{
return $request->wantsJson()
? new JsonResponse(['message' => trans($this->status)], 200)
: redirect()->route('login')->with('status', trans($this->status));
}
这是一个有用的链接,显示了 Fortify 在撰写本文时使用的所有响应类: Overriding other Jetstream and Fortify functionality
【讨论】: