首页 > 解决方案 > Laravel 5.7 - 如何动态触发用户验证电子邮件?

问题描述

当我使用注册页面注册时,我收到的电子邮件中的链接可以正常工作。

当我$user->sendEmailVerificationNotification() 像在 中看到的那样使用时vendor\laravel\framework\src\Illuminate\Auth\Listeners\SendEmailVerificationNotification.php,它不起作用。

我收到了电子邮件,但是当我单击验证链接时,我被重定向到登录页面并且用户没有得到验证。

用例:我的系统有一个超级用户,他可以添加用户,我希望触发该电子邮件以供用户验证自己。

任何帮助,将不胜感激。

标签: phplaravel

解决方案


这个答案假设你已经设置了 Laravel 的AuthenticationEmail Verification

当用户在应用程序上注册时,控制器默认使用Illuminate\Foundation\Auth\RegistersUsertrait。请注意该方法中发生的附加功能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));

更多信息:


推荐阅读