簡體   English   中英

在Laravel上更改密碼重置的電子郵件視圖路徑

[英]Change email view path for password reset on Laravel

使用Laravel 5,我需要2個不同的密碼重置電子郵件視圖。 電子郵件視圖的默認路徑為emails.password 但在某些情況下,我想發送電子郵件.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());
    });
}

對於對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/

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

    這是一個更丑陋的解決方案,如果你有自動化測試,這將是一個痛苦的工作。

免責聲明 :我沒有測試過任何此類代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM