首页 文章

在Laravel上更改密码重置的电子邮件视图路径

提问于
浏览
5

使用Laravel 5,我需要2个不同的密码重置电子邮件视图 . 电子邮件视图的默认路径为 emails.password . 但在某些情况下,我想发送 emails.password_alternative .

我怎样才能做到这一点? (来自Laravel的PasswordBroker)

这是我目前的代码:

public function __construct(Guard $auth, PasswordBroker $passwords)
{
    $this->auth = $auth;
    $this->passwords = $passwords;
}

public function sendReset(PasswordResetRequest $request)
{
    //HERE : If something, use another email view instead of the default one from the config file
    $response = $this->passwords->sendResetLink($request->only('email'), function($m)
    {
        $m->subject($this->getEmailSubject());
    });
}

2 回答

  • 7

    使用PasswordBroker并基于Illuminate/Auth/Passwords/PasswordBroker.php类, $emailView 是受保护的变量,因此在实例化类后,您无法更改该值 .

    但是,您有几个解决方案:

    • 您可以创建自己的类来扩展PasswordBroker并使用它 .
    class MyPasswordBroker extends PasswordBroker {
        public function setEmailView($view) {
            $this->emailView = $view;
        }
    }
    
    // (...)
    
    public function __construct(Guard $auth, MyPasswordBroker $passwords)
    {
        $this->auth = $auth;
        $this->passwords = $passwords;
    }
    
    public function sendReset(PasswordResetRequest $request)
    {
        if ($someConditionHere) {
            $this->passwords->setEmailView('emails.password_alternative');
        }
        $response = $this->passwords->sendResetLink($request->only('email'), function($m)
        {
            $m->subject($this->getEmailSubject());
        });
    }
    
    • 您可以在方法中创建PasswordBroker,而无需使用依赖注入 .
    public function sendReset(PasswordResetRequest $request)
    {
        $emailView = 'emails.password';
    
        if ($someConditionHere) {
            $emailView = 'emails.password_alternative';
        }
    
        $passwords = new PasswordBroker(
            App::make('TokenRepositoryInterface'),
            App::make('UserProvider'),
            App::make('MailerContract'),
            $emailView
        );
    
        $response = $passwords->sendResetLink($request->only('email'), function($m)
        {
            $m->subject($this->getEmailSubject());
        });
    }
    

    这是一个更丑陋的解决方案,如果你有自动化测试,这将是一个痛苦的工作 .

    免责声明:我没有测试过任何此类代码 .

  • 4

    对于对Laravel 5.2感兴趣的任何人,您可以通过添加设置自定义html和文本电子邮件视图以重置密码

    config(['auth.passwords.users.email' => ['auth.emails.password.html', 'auth.emails.password.text']]);
    

    在中间件调用之前,在构造函数中的PasswordController.php .

    这将覆盖PasswordBroker的app / config / auth.php设置 .

    密码重置电子邮件的刀片模板位于:

    yourprojectname / resources / views / auth / emails / password / html.blade.php yourprojectname / resources / views / auth / emails / password / text.blade.php

    花了我很长时间 .

    积分:http://ericlbarnes.com/2015/10/14/how-to-send-both-html-and-plain-text-password-reset-emails-in-laravel-5-1/ http://academe.co.uk/2014/01/laravel-multipart-registration-and-reminder-emails/

相关问题