如何使用 Laravel 5 中的队列通过电子邮件发送密码重置链接 [英] How to send the password reset link via email using queue in laravel 5

查看:34
本文介绍了如何使用 Laravel 5 中的队列通过电子邮件发送密码重置链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 laravel 的 ResetsPasswords 特性来实现密码重置.我想实现的是使用队列发送电子邮件.仔细研究代码,我在函数 postEmail() 中找到了以下行:

I am using the ResetsPasswords trait by laravel to implement password reset. What I would like to achieve is to send the email using queue. Digging through the code I found the line below in function postEmail():

$response = Password::sendResetLink($request->only('email'), function (Message $message) {
            $message->subject($this->getEmailSubject());
        }); 

进一步挖掘我注意到 sendResetLink() 函数是在 PasswordBroker 类中实现的,该类又调用函数 emailResetLink().emailResetLink 函数返回以下内容:

Digging further I notice that sendResetLink() function is implemented in a PasswordBroker class which in turn calls the function emailResetLink(). emailResetLink function returns the following:

return $this->mailer->send($view, compact('token', 'user'), function ($m) use ($user, $token, $callback) {
            $m->to($user->getEmailForPasswordReset());

我可以简单地将 mailer->send 更改为 mailer->queue.他们有什么更好的方法可以在不修改这个非项目文件的情况下做到这一点吗?

which I can simply change mailer->send to mailer->queue. Is they some better way to do it without modifying this non-project file?

推荐答案

这就是 Laravel 容器派上用场的地方.如果您不喜欢某个核心组件的功能,那么您可以继续轻松地覆盖它.

This is where the Laravel container comes to the rescue. If you don't like the functionality of a core component then you can go ahead and override it fairly painlessly.

首先,您需要创建自己的 PasswordBroker:

First things first you will need to create your own PasswordBroker:

namespace AppAuthPasswords;

use IlluminateAuthPasswordsPasswordBroker as IlluminatePasswordBroker;

class PasswordBroker extends IlluminatePasswordBroker
{

    public function emailResetLink()
    {
        $view = $this->emailView;

        return $this->mailer->queue($view, compact('token', 'user'), function ($m) use ($user, $token, $callback) {
            $m->to($user->getEmailForPasswordReset());
            if (! is_null($callback)) {
                call_user_func($callback, $m, $user, $token);
            }
        });
    }

}

如果您想将命名空间放置在应用中的其他位置,请将其更改为您想要的任何名称.

Change your namespace to whatever you want if you want to place it elsewhere in your app.

由于注册服务的服务提供商是延期服务提供商,您将需要创建自己的提供程序来替换它.可能最简单的方法是使用以下内容扩展 IlluminateAuthPasswordsPasswordResetServiceProvider :

Since the service provider registering the service is a deferred service provider you will need to create your own provider to replace it. Probably the easiest way to do this is extend IlluminateAuthPasswordsPasswordResetServiceProvider with something like the following:

namespace AppProviders;

use AppAuthPasswordsPasswordBroker;

class PasswordResetServiceProvider extends IlluminateAuthPasswordsPasswordResetServiceProvider
{

    protected function registerPasswordBroker()
    {
        $this->app->singleton('auth.password', function ($app) {
            $tokens = $app['auth.password.tokens'];

            $users = $app['auth']->driver()->getProvider();

            $view = $app['config']['auth.password.email'];

            return new PasswordBroker(
                $tokens, $users, $app['mailer'], $view
            );
        });
    }

}

最后在你的 config/app.php 文件中删除 IlluminateAuthPasswordsPasswordResetServiceProvider::class 并添加 AppProvidersPasswordResetServiceProvider::class 到您的 'providers' 数组.

Finally in your config/app.php file remove IlluminateAuthPasswordsPasswordResetServiceProvider::class and add AppProvidersPasswordResetServiceProvider::class to your 'providers' array.

Laravel 现在将使用您的 PasswordBroker 实现而不是现有框架,您不必担心修改框架代码.

Laravel will now use your implementation of the PasswordBroker rather than the stock framework one and you don't have to worry about modifying framework code.

这篇关于如何使用 Laravel 5 中的队列通过电子邮件发送密码重置链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆