php - Laravel 5.7 - 如何动态触发用户验证电子邮件?
问题描述
当我使用注册页面注册时,我收到的电子邮件中的链接可以正常工作。
当我$user->sendEmailVerificationNotification()
像在 中看到的那样使用时vendor\laravel\framework\src\Illuminate\Auth\Listeners\SendEmailVerificationNotification.php
,它不起作用。
我收到了电子邮件,但是当我单击验证链接时,我被重定向到登录页面并且用户没有得到验证。
用例:我的系统有一个超级用户,他可以添加用户,我希望触发该电子邮件以供用户验证自己。
任何帮助,将不胜感激。
解决方案
这个答案假设你已经设置了 Laravel 的Authentication和Email Verification。
当用户在应用程序上注册时,控制器默认使用Illuminate\Foundation\Auth\RegistersUser
trait。请注意该方法中发生的附加功能register()
,并注意生成了一个\Illuminate\Auth\Events\Registered
事件,该事件又触发了侦听 SendEmailVerificationNotification
器(您当前从中获取代码的位置。)
对于您的自定义类,您可能会重用其上的Illuminate\Foundation\Auth\RegistersUser
特征,但这可能会让人感到奇怪,因为您的类可能不是控制器,并且特征涉及额外的控制器特定逻辑。
相反,您可以尝试Illuminate\Foundation\Auth\RegistersUser::register()
从新类中提取一些代码并使用它。
所以,类似于:
// If you do not yet have a new user object.
$user = \App\User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => \Illuminate\Support\Facades\Hash::make($data['password']),
]);
// Fire the event now with the user you want to receive an email.
event(new \Illuminate\Auth\Events\Registered($user));
更多信息:
推荐阅读
- xamarin.forms - Xamarin 表单自定义控件仅在更改 ItemSelected 时通知模型
- html - 404文件夹外的链接
- php - 登录站点的 PHP 会话 - 理论
- c# - 尝试处理菜单和电影
- docker - Kubernetes Hostalias 重定向到没有请求标头的 Kubernetes 服务
- eclipse - Eclipse 初学者问题 - 通过文件资源管理器的 SSH shell 未按预期工作
- amazon-web-services - 如何设计关键模式以使每个应用程序只有一个 DynamoDB 表?
- cordova - 如何使threejs画布不被键盘调整大小
- c# - C# WindowsForms PictureBox:控制坐标和图像中像素位置之间的转换
- c# - 为什么不调用这个 C# 实例构造函数,除非有对非静态成员的引用?