我想使用自定义模板来发送由 Laravel 11 发送的验证电子邮件。 它会生成一个验证 url,但是当我单击它时,它会在浏览器上显示 404。
下面是我的用户模型,我在其中生成 URL 并传递给我的自定义通知类:
public function verificationUrl()
{
$user = $this;
$hash = urlencode(Hash::make($user->email));
return URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(config('auth.verification.expire', 60)),
['id' => $user->getKey(), 'hash' => $hash]
);
}
public function sendEmailVerificationNotification()
{
$verificationUrl = $this->verificationUrl();
$this->notify(new \App\Notifications\VerifyEmail($verificationUrl));
}
我收到的电子邮件的 URL 如下:
http://127.0.0.1:8000/email/verify/17/%242y%2412%24pYgHVLqVp49BRu%2FyA40Me.CmogjO17sMc2IG06VuKQj4gnElWSBTu?expires=1717544173&signature=16ad181c7b8f733077d4de1dda24e6d65663223bb4ab36608e51f63cda74328e
这是我的
web.php
路线:
Route::get('/email/verify/{id}/{hash}', [EmailVerificationController::class, 'verify'])
->middleware(['auth', 'signed'])
->name('verification.verify');
这是我的控制器:
public function verify(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return redirect()->route('dashboard'); // or any other route you want to redirect if already verified
}
if ($request->user()->markEmailAsVerified()) {
event(new Verified($request->user()));
}
return redirect()->route('dashboard')->with('verified', true);
}
我正在寻求一些帮助来找出我哪里出了问题。生成的默认网址适用于
$request->fulfill();
。
虽然您已经使用签名 URL 方法成功生成了
URL
并且您的 route
定义是正确的,但您没有使用 route
的 verify
方法正确映射 EmailVerificationController
。
您需要在
route
方法的 Request $request
实例之后传递 verify
参数。下面给出了正确的方法签名
public function verify(Request $request,$id,$hash)
{
.........
.........
.........
}
试试这个。我认为它会起作用。
有关 Laravel Route 的更多信息可以查看官方文档。