【问题标题】:Change email view path for password reset on Laravel在 Laravel 上更改密码重置的电子邮件视图路径
【发布时间】:2016-07-14 11:49:00
【问题描述】:

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

【问题讨论】:

    标签: php email laravel passwords


    【解决方案1】:

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

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

    到中间件调用之前构造函数中的PasswordController.php。

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

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

    您的项目名称/资源/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/

    【讨论】:

      【解决方案2】:

      使用PasswordBroker 并基于Illuminate/Auth/Passwords/PasswordBroker.php 类,$emailView 是一个受保护的变量,因此一旦类被实例化就不能更改值。

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

      1. 您可以创建自己的类来扩展 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());
            });
        }
        
      2. 您可以在您的方法中创建 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());
            });
        }
        

        这是一个更丑陋的解决方案,如果您有自动化测试,使用起来会很痛苦。

      免责声明:我没有测试过这段代码。

      【讨论】:

      • 哦,我忘了说我使用的是 Laravel PasswordBroker
      • 变量没有设置器的情况下受到保护是否有原因?还是 Laravel 将其设置为受保护是因为他们没有想到人们愿意更改它的情况?
      • 我猜他们没有想到这一点。找出答案的最好方法是创建一个 GitHub 问题并与 Laravel 的主要开发人员讨论。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2019-12-06
      相关资源
      最近更新 更多