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

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

问题描述

我正在使用laravel的ResetsPasswords特征来实现密码重置。我想要实现的是使用队列发送电子邮件。挖掘代码我在函数postEmail()中找到下面的行:

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

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

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

我可以简单地将 mailer->发送更改为 mailer-> queue 。是否有更好的方法来修改这个非项目文件?

解决方案

这是Laravel集装箱来救援的地方,如果你不喜欢核心组件的功能,那么你可以继续前进,无条件地覆盖它。


$首先你需要创建自己的PasswordBroker:

 命名空间App \Auth\Passwords ; 

使用Illuminate\Auth\Passwords\PasswordBroker作为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);
}
});
}

}

将您的命名空间更改为任何您想要的如果你想把它放在你的应用程序的其他地方。



由于服务提供商注册服务是一个延期服务提供者,您将需要创建自己的提供者来替换它。可能最简单的方法是扩展 Illuminate\Auth\Passwords\PasswordResetServiceProvider ,其中包含以下内容:

 命名空间App \Providers; 

使用App \Auth\Passwords\PasswordBroker;

class PasswordResetServiceProvider extends \Illuminate\Auth\Passwords\PasswordResetServiceProvider
{

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'] ;

返回新的PasswordBroker(
$ tokens,$ users,$ app ['mailer'],$ view
);
});
}

}

最后在你的 config / app.php file remove Illuminate\Auth\Passwords\PasswordResetServiceProvider :: class 并添加 App \Providers\PasswordResetServiceProvider :: class 到你的'provider'数组。



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


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());
        }); 

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

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

解决方案

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.

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

namespace App\Auth\Passwords;

use Illuminate\Auth\Passwords\PasswordBroker 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.

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 Illuminate\Auth\Passwords\PasswordResetServiceProvider with something like the following:

namespace App\Providers;

use App\Auth\Passwords\PasswordBroker;

class PasswordResetServiceProvider extends \Illuminate\Auth\Passwords\PasswordResetServiceProvider
{

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

}

Finally in your config/app.php file remove Illuminate\Auth\Passwords\PasswordResetServiceProvider::class and add App\Providers\PasswordResetServiceProvider::class to your 'providers' array.

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天全站免登陆