2016-09-05 59 views
11

如何在Laravel 5.3中自定義重置電子郵件模板的路徑?Laravel 5.3重新定義重置電子郵件刀片模板

使用的模板是:vendor/laravel/framework/src/Illuminate/Notifications/resources/views/email.blade.php

我想建立自己的。

此外,如何改變這種電子郵件的預定義的文本:vendor/laravel/framework/src/Illuminate/Auth/Notifications/ResetPassword.php

public function toMail() 
{ 
    return (new MailMessage) 
     ->line([ 
      'You are receiving this email because we received a password reset request for your account.', 
      'Click the button below to reset your password:', 
     ]) 
     ->action('Reset Password', url('password/reset', $this->token)) 
     ->line('If you did not request a password reset, no further action is required.'); 
} 

回答

28

要更改模板,你應該使用工匠命令php artisan vendor:publish它會在你resources/views/vendor目錄中創建刀模板。要更改電子郵件的文本,您應該覆蓋用戶模型上的sendPasswordResetNotification方法。這裏描述https://laravel.com/docs/5.3/passwords重置電子郵件定製部分。

您必須將新方法添加到您的用戶模型中。

public function sendPasswordResetNotification($token) 
{ 
    $this->notify(new ResetPasswordNotification($token)); 
} 

並使用自己的類來通知,而不是ResetPasswordNotification。

修訂:對@ lewis4u要求

一步一步的指示:

  1. 要創建新的Notification類,則必須使用此命令行php artisan make:notification MyResetPassword。它會在app/Notifications目錄下創建一個新的Notification Class'MyResetPassword'。

  2. 附加use App\Notifications\MyResetPassword;到您的用戶模型

  3. 添加新的方法,以您的用戶模型。

    public function sendPasswordResetNotification($token) 
    { 
        $this->notify(new MyResetPassword($token)); 
    } 
    
  4. 運行PHP的工匠命令php artisan vendor:publish --tag=laravel-notifications運行此命令後,郵件通知模板將位於資源/視圖/供應商/通知目錄。

  5. 如果需要,請編輯您的MyResetPassword類方法toMail()。這裏描述的是https://laravel.com/docs/5.3/notifications

  6. 如果需要編輯電子郵件刀片模板。這是resources/views/vendor/notifications/email.blade.php

獎勵: Laracast視頻:https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/9

PS:感謝@ Garric15的建議有關php artisan make:notification

+1

另外,創建一個新的Notification類,則必須使用此命令行' php artisan make:通知MyOwnResetPassword' [更多信息](https://laracasts.com/discuss/channels/laravel/how-to-override-message-in-sendresetlinkemail-in-forgotpasswordcontroller#reply-183598) – Garric15

8

我想闡述一個非常有益的Eugen’s answer,但沒有足夠的聲望發表評論。

如果您想擁有自己的目錄結構,則不必使用發佈到views/vendor/notifications/..的刀片模板。當你創建一個新的Notification類,並開始建立你的MailMessage類,它有一個view()方法,你可以用它來替代默認的觀點:

/** 
* Get the mail representation of the notification. 
* 
* @param mixed $notifiable 
* @return \Illuminate\Notifications\Messages\MailMessage 
*/ 
public function toMail($notifiable) 
{ 
    return (new MailMessage) 
     ->view('emails.password_reset'); 
     // resources/views/emails/password_reset.blade.php will be used instead. 
} 
+0

謝謝!浪費整晚試圖弄清楚這一點,這是很容易.. pff .... –