Way*_*her 13 php email email-verification laravel
我刚刚升级到Laravel 5.7,现在我正在使用内置的电子邮件验证.然而,有两件事我无法弄清楚,主要问题是我如何自定义发送给用户以验证其电子邮件的电子邮件?如果用户更改了他们的电子邮件,我也无法弄清楚如何发送该电子邮件,但我可以将其保存为另一个线程.
Yve*_*ndo 18
如果要在Laravel 5.7中添加电子邮件验证,建议的方法是在模型上实现Illuminate\Contracts\Auth\MustVerifyEmail和使用Illuminate\Auth\MustVerifyEmail特征App\User.
要创建一些自定义行为,您可以覆盖方法sendEmailVerificationNotification,该方法是通过调用方法通知创建的用户的方法notify,并作为参数传递Notifications\MustVerifyEmail类的新实例.
您可以创建一个自定义通知,该通知将作为参数传递到$this->notify()用户模型中的sendEmailVerificationNotification方法中:
public function sendEmailVerificationNotification()
{
$this->notify(new App\Notifications\CustomVerifyEmail);
}
Run Code Online (Sandbox Code Playgroud)
...然后在您的CustomVerifyEmail通知中,您可以定义验证的处理方式.您可以通过发送带有自定义verification.route的电子邮件来通知创建的用户,该电子邮件将采用您想要的任何参数.
电子邮件验证通知流程
当新用户注册时,会在该Illuminate\Auth\Events\Registered事件中发出一个事件App\Http\Controllers\Auth\RegisterController并且该Registered事件具有一个Illuminate\Auth\Listeners\SendEmailVerificationNotification在以下位置注册的侦听器App\Providers\EventServiceProvider:
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
]
];
Run Code Online (Sandbox Code Playgroud)
该SendEmailVerificationNotification监听器会检查$用户-这是作为参数传递给new Registered($user = $this->create($request->all()))在Laravel默认身份验证App\Http\Controllers\Auth\RegisterController-是一个实例Illuminate\Contracts\Auth\MustVerifyEmail,其是Laravel建议在使用特征的名称App\User型号,当你想提供默认的电子邮件验证,也检查$user尚未验证.如果全部通过,sendEmailVerificationNotification则在该用户上调用该方法:
if ($event->user instanceof MustVerifyEmail && !$event->user->hasVerifiedEmail()) {
$event->user->sendEmailVerificationNotification();
}
Run Code Online (Sandbox Code Playgroud)
我认为执行此操作的简单方法是使用此处的文档发出新通知:https : //laravel.com/docs/5.7/notifications#creating-notifications
然后覆盖函数:
public function sendEmailVerificationNotification()
{
$this->notify(new App\Notifications\CustomEmailNotification);
}
Run Code Online (Sandbox Code Playgroud)
在用户模型中。
或者你可以
php artisan vendor:publish --tag=laravel-notifications
Run Code Online (Sandbox Code Playgroud)
这会将模板复制到resources / views / vendor / notifications目录,您可以在此处进行修改
不幸的是,发送的这封电子邮件不是来自“视图”,它Notification实际上是内嵌的。这是它目前在需要发送时建造的地方:Illuminate\Auth\Notifications\VerifyEmail@toMail。这个特定的类有一个静态回调,可以设置它来构建这封电子邮件,而不是让它去做。
在方法中的服务提供者中,boot您需要为此类分配回调:
像这样的东西可能会起作用:
public function boot()
{
\Illuminate\Auth\Notifications\VerifyEmail::toMailUsing(function ($notifiable) {
// this is what is currently being done
// adjust for your needs
return (new \Illuminate\Notifications\Messages\MailMessage)
->subject(\Lang::getFromJson('Verify Email Address'))
->line(\Lang::getFromJson('Please click the button below to verify your email address.'))
->action(
\Lang::getFromJson('Verify Email Address'),
$this->verificationUrl($notifiable)
)
->line(\Lang::getFromJson('If you did not create an account, no further action is required.'));
});
}
Run Code Online (Sandbox Code Playgroud)
由于这是一条通知,因此您应该有更多自定义选项。
如果你想使用你自己的Notification类,你可以覆盖( ) 模型sendEmailVerificationNotification上的方法(这是来自trait)。UserAuthenticatableMustVerifyEmail
第二个问题:
您应该拥有的VerificationController( App\Http\Controllers\Auth\VerificationController) 有一个名为resend(来自 trait VerifiesEmails)的方法,看起来很适合用于此目的。
您应该通过以下方式为这些验证路线设置路线 Auth::routes(['verify' => true]);
笔记:
验证系统使用5.7users表email_verified_at中的一个字段来标记这一点。你会想确保你有这个字段。当用户更改电子邮件地址时,我想您可以将null其重定向到resend路由,以发送新的验证。这将使它们处于“未验证”状态,直到它们重新验证为止,如果这是您打算发生的情况。
更新:
看来我们正走在正确的轨道上。我发现这个 SO 答案涵盖了类似的事情:
在 laravel 5.7 中更改验证电子邮件的默认“主题”字段
我将向您展示如何使用自定义视图从头开始自定义用户验证电子邮件,而不使用任何供应商发布
\n步骤1
\n创建新通知UserVerifyNotification class。VerifyEmail class它应该从图书馆 延伸Illuminate\\Auth\\Notifications\\VerifyEmail;
代码 :
\n use Illuminate\\Auth\\Notifications\\VerifyEmail;\n ...\n class UserVerifyNotification extends VerifyEmail implements ShouldQueue\n {\n use Queueable;\n public $user; //you'll need this to address the user\n \n /**\n * Create a new notification instance.\n *\n * @return void\n */\n public function __construct($user='')\n {\n $this->user = $user ?: Auth::user(); //if user is not supplied, get from session\n }\n \n /**\n * Get the notification's delivery channels.\n *\n * @param mixed $notifiable\n * @return array\n */\n public function via($notifiable)\n {\n return ['mail'];\n }\n \n /**\n * Get the mail representation of the notification.\n *\n * @param mixed $notifiable\n * @return \\Illuminate\\Notifications\\Messages\\MailMessage\n */\n public function toMail($notifiable)\n {\n $actionUrl = $this->verificationUrl($notifiable); //verificationUrl required for the verification link\n $actionText = 'Click here to verify your email';\n return (new MailMessage)->subject('Verify your account')->view(\n 'emails.user-verify',\n [\n 'user'=> $this->user,\n 'actionText' => $actionText,\n 'actionUrl' => $actionUrl,\n ]);\n }\n \n /**\n * Get the array representation of the notification.\n *\n * @param mixed $notifiable\n * @return array\n */\n public function toArray($notifiable)\n {\n return [\n //\n ];\n }\n \n }\n \n\n \nRun Code Online (Sandbox Code Playgroud)\n第2步
\n为内部电子邮件 ( user-verify.blade.php )创建刀片视图resources\\views\\emails
<DOCTYPE html>\n <html lang="en-US">\n \n <head>\n <meta charset="utf-8">\n </head>\n \n <body>\n <p>Dear {{$user->name}},</p>\n <p>\n Please click the button below to verify your email address.\n </p>\n \n \n <a href="{{ $actionUrl }}" class="button">{{$actionText}}</a>\n \n <p>If you did not create an account, no further action is required.</p>\n \n <p>\n Best regards, <br>\n \n {{ config('app.name')}}\n </p>\n \n <p>\n <hr>\n <span class="break-all">\n <strong>If you\xe2\x80\x99re having trouble clicking the link, copy and paste the URL below into your web browser:</strong><br/>\n <em>{{$actionUrl}}</em>\n </p>\n \n \n \n </body>\n \n </html>\nRun Code Online (Sandbox Code Playgroud)\n步骤:3
\n在里面添加如下方法User model
class User extends Authenticatable implements MustVerifyEmail\n {\n use HasFactory, Notifiable;\n\n ...\n ...\n ...\n\n public function sendEmailVerificationNotification()\n {\n $this->notify(new \\App\\Notifications\\UserVerifyNotification(Auth::user())); //pass the currently logged in user to the notification class\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n解释
\nIlluminate\\Auth\\EventsEventServiceProvider( App\\Providers\\EventServiceProvider),监听 Registered 事件。 1. 注册完成后,调用SendEmailVerificationNotification方法 ( Illuminate\\Auth\\Listeners\\SendEmailVerificationNotification) 。sendEmailVerificationNotification()库中的方法Illuminate\\Auth\\Listeners\\SendEmailVerificationNotificationsendEmailVerificationNotification()步骤 3 中的函数,并表明我们想要使用我们之前在步骤 1 中创建的自己的通知类 Auth::routes([\n 'verify' => true,\n 'register' => true,\n ]);\nRun Code Online (Sandbox Code Playgroud)\n
小智 5
快速简便的方法:
php artisan vendor:publish --tag=laravel-notifications
Run Code Online (Sandbox Code Playgroud)
它正在创建一个新文件:
\resources\views\vendor\notifications
Run Code Online (Sandbox Code Playgroud)
这是 Laravel 的电子邮件模板。您可以更改和自定义它。
| 归档时间: |
|
| 查看次数: |
14986 次 |
| 最近记录: |