laravel - 在 laravel 5.7 的验证电子邮件中更改邮件验证链接中的自定义参数的“VerifyEmail”类,用于方法 verifyUrl()
问题描述
laravel 5.7 自带的验证邮件。我需要如何以及在哪里更改它?我已经在网上搜索过,但是因为它是5.7中的全新功能,所以我找不到答案。你能帮我吗?提前致谢。
基本上那个类在 Illuminate\Auth\Notifications 下
我想覆盖其中一种方法:
class VerifyEmail extends Notification
{
// i wish i could override this method
protected function verificationUrl($notifiable)
{
return URL::temporarySignedRoute('verification.verify', Carbon::now()->addMinutes(60), ['id' => $notifiable->getKey()]);
}
}
解决方案
因为您的User
模型使用Illuminate\Auth\MustVerifyEmail
您可以覆盖该方法sendEmailVerificationNotification
,该方法是通过调用该方法通知创建的用户notify
并作为参数传递Notifications\MustVerifyEmail
该类的新实例的方法。
您可以创建一个自定义通知,它将作为参数传递给模型$this->notify()
中的sendEmailVerificationNotification
方法User
:
public function sendEmailVerificationNotification()
{
$this->notify(new App\Notifications\CustomVerifyEmail);
}
在您的CustomVerifyEmail
通知中,您可以定义route
处理验证的方式以及它将采用的所有参数。
当新用户注册时,Illuminate\Auth\Events\Registered
会在 中发出一个事件,App\Http\Controllers\Auth\RegisterController
并且该事件有一个Illuminate\Auth\Listeners\SendEmailVerificationNotification
在 中注册的侦听器App\Providers\EventServiceProvider
:
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
]
];
此侦听器检查在 Laravel 默认身份验证中$user
作为参数传递给的是否是 Laravel 建议在模型中使用的特征的实例,当您想提供默认电子邮件验证并检查还没有已验证。如果所有这些都通过,它将调用该用户的方法:new Registered($user = $this->create($request->all()))
App\Http\Controllers\Auth\RegisterController
Illuminate\Contracts\Auth\MustVerifyEmail
App\User
$user
sendEmailVerificationNotification
if ($event->user instanceof MustVerifyEmail && !$event->user->hasVerifiedEmail()) {
$event->user->sendEmailVerificationNotification();
}
推荐阅读
- java - 使用 jdk.compiler 模块获取值作为注释的类
- javascript - 如何将 MJML 上的相对路径与 mjml-react 一起用于电子邮件模板
- mysql - 如何在 MySQL 中查询可扩展的价格
- c++ - 子类访问时超类成员指针变为空
- python - 如何解决这个问题?超出 HTTPConnectionPool 最大重试次数(NewConnectionError 无法建立新连接:[Errno 11002] getaddrinfo failed)
- php - 错误:stdClass 类的对象无法转换为字符串
- php - 如何使用查询生成器获取几乎与 sql 中列中的单词匹配的单词
- node.js - 使用 Node.js 下载和执行 .exe 文件时出现 EBUSY 错误
- android - 使用 gitLab ci/cd 时如何自动动态指定应用程序名称
- javascript - 使用 Javascript 将 Textarea 的 MaxLength 设置为 50