dshgdhdfcas30210 2019-02-11 23:03
浏览 95

Laravel 5.7 - 如何动态触发用户验证电子邮件?

When I register using the registration page, the link in the email that I get works fine.

When I instead use $user->sendEmailVerificationNotification() like I saw in vendor\laravel\framework\src\Illuminate\Auth\Listeners\SendEmailVerificationNotification.php, it does not work.

I get the email, but when I click on the verify link, I get redirected to the login page and the user does not get verified.

Use Case: My system has a superuser, who can add users, and I am hoping to fire that email for the users to verify themselves.

Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • douzhuanqian8244 2019-02-11 23:51
    关注

    This answer assumes you've already setup Laravel's Authentication and Email Verification.

    When a user registers on the application, the controller by default is using the Illuminate\Foundation\Auth\RegistersUser trait. Notice the additional functionality that takes place in the register() method there, and also notice that a \Illuminate\Auth\Events\Registered event is generated which in turns triggers the listener SendEmailVerificationNotification (where you are currently getting your code from.)

    For your custom class, you could potentially reuse the Illuminate\Foundation\Auth\RegistersUser trait on it, but that could feel strange as your class is probably not a controller and the trait involves extra controller-specific logic.

    Instead, you could try ripping some code from Illuminate\Foundation\Auth\RegistersUser::register() and use it within your new class.

    So, something similar to:

    // 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));
    

    More information:

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效