2017-03-03 143 views
3

我想在Laravel中定製密碼重置電子郵件。定製忘記密碼郵件Laravel 5.4

我不得不覆蓋這個功能:

namespace Illuminate\Auth\Passwords; 

use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification; 
use Illuminate\Http\Request; 


trait CanResetPassword 
{ 
    /** 
    * Get the e-mail address where password reset links are sent. 
    * 
    * @return string 
    */ 
    public function getEmailForPasswordReset() 
    { 
     return $this->email; 
    } 

    /** 
    * Send the password reset notification. 
    * 
    * @param string $token 
    * @return void 
    */ 

public function sendPasswordResetNotification($token) 
{ 

    $this->notify(new ResetPasswordNotification($token)); 

} 

這是我的嘗試:

public function sendPasswordResetNotification($token, Requests $request) 
{ 
Mail::to($request->email)->send(new newpassword($token)); 
} 

我得到這個錯誤:

Declaration of Illuminate\Foundation\Auth\User::sendPasswordResetNotification($token, Illuminate\Http\Request $request) must be compatible with Illuminate\Contracts\Auth\CanResetPassword::sendPasswordResetNotification($token)

+0

退房這個答案http://stackoverflow.com/questions/40574001/how-to-change-reset-password-email -subject功能於laravel –

回答

4

如果你讀了錯誤,它告訴您的班級與CanResetPassword不兼容。如果你看那個....

interface CanResetPassword 
{ 
    /** 
    * Get the e-mail address where password reset links are sent. 
    * 
    * @return string 
    */ 
    public function getEmailForPasswordReset(); 
    /** 
    * Send the password reset notification. 
    * 
    * @param string $token 
    * @return void 
    */ 
    public function sendPasswordResetNotification($token); 
} 

你可以看到功能sendPasswordResetNotification應該只需要一個參數,$token。因此,您需要從方法的簽名中刪除Request $request作爲參數。

爲了獲得請求,您需要使用sendPasswordResetNotification方法中的函數request()

public function sendPasswordResetNotification($token) 
{ 
    Mail::to(request()->email)->send(new newpassword($token)); 
} 
4

我很驚訝你會想要這個長度來定製電子郵件。

試試這個:

php artisan vendor:publish 

然後修改此文件

/resources/views/vendor/notifications/email.blade.php 

偉大的作品爲我們的使用。

[email protected]:~/laravel_5.4$ php artisan vendor:publish 
Copied Directory [/vendor/laravel/framework/src/Illuminate/Pagination/resources/views] To [/resources/views/vendor/pagination] 
Copied Directory [/vendor/laravel/framework/src/Illuminate/Notifications/resources/views] To [/resources/views/vendor/notifications] 
Copied Directory [/vendor/laravel/framework/src/Illuminate/Mail/resources/views] To [/resources/views/vendor/mail] 
Publishing complete. 

現在,如果你需要改變副本,你想,原來ResetPassword類使用花哨的按鈕,你可以像下面的例子中你user.php的類擴展郵件類。

這裏是我們的一個副本僅僅作爲示例來的偉大工程:

<?php 

namespace App; 

use Illuminate\Foundation\Auth\User as Authenticatable; 
use Illuminate\Notifications\Notifiable; 
use Illuminate\Auth\Notifications\ResetPassword; 
use Illuminate\Notifications\Messages\MailMessage; 

class User extends Authenticatable 
{ 
    use Notifiable; 

    protected $table = 'Users'; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'firstName', 
     'lastName', 
     'email', 
     'password', 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 

    /** 
    * Sends the password reset notification. 
    * 
    * @param string $token 
    * 
    * @return void 
    */ 
    public function sendPasswordResetNotification($token) 
    { 
     $this->notify(new CustomPassword($token)); 
    } 
} 

class CustomPassword extends ResetPassword 
{ 
    public function toMail($notifiable) 
    { 
     return (new MailMessage) 
      ->line('We are sending this email because we recieved a forgot password request.') 
      ->action('Reset Password', url(config('app.url') . route('password.reset', $this->token, false))) 
      ->line('If you did not request a password reset, no further action is required. Please contact us if you did not submit this request.'); 
    } 
}