2016-11-13 4 views
10

私はLaravelの初心者です。現在、私はこのフレームワークを学んでいます。私のcurent Laravelのバージョンは5.3です。laravelでリセットパスワードの電子メールの件名を変更するには?

私はphp artisan make:authを使用して私の認証を足場を張っています。すべて正常に動作しています。また、Gmailのsmtpを.envファイルで、mail.phpをconfig directgor​​yで設定しました。すべてが完全に機能しています。しかし、デフォルトでパスワードを忘れてしまったというメールの件名はReset Passwordになっています。私はそれを変えたい。

私はいくつかのブログを見ました。私はいくつかのブログを見つけた。私は自分のサイトに実装しています。しかし、同じ出力が来る。私はこれらのリンクに続く

-

https://laracasts.com/discuss/channels/general-discussion/laravel-5-password-reset-link-subject

https://laracasts.com/discuss/channels/general-discussion/reset-password-email-subject

https://laracasts.com/discuss/channels/laravel/how-to-override-message-in-sendresetlinkemail-in-forgotpasswordcontroller

答えて

25

パスワードリセットのメールの件名を変更することはできますが、余分な作業が必要になります。まず、ResetPassword通知の独自の実装を作成する必要があります。

のはResetPassword.phpそれを名付けてみましょう、app\Notificationsディレクトリ内に新しい通知クラスを作成します。

<?php 

namespace App\Notifications; 

use Illuminate\Notifications\Notification; 
use Illuminate\Notifications\Messages\MailMessage; 

class ResetPassword extends Notification 
{ 
    public $token; 

    public function __construct($token) 
    { 
     $this->token = $token; 
    } 

    public function via($notifiable) 
    { 
     return ['mail']; 
    } 

    public function toMail($notifiable) 
    { 
     return (new MailMessage) 
      ->subject('Your Reset Password Subject Here') 
      ->line('You are receiving this email because we received a password reset request for your account.') 
      ->action('Reset Password', url('password/reset', $this->token)) 
      ->line('If you did not request a password reset, no further action is required.'); 
    } 
} 

ます。また、職人のコマンドを使用して通知テンプレートを生成することができます。

php artisan make:notification ResetPassword 

それとも、単にコピー&ペーストをすることができます上記のコード。この通知クラスは、デフォルトのIlluminate\Auth\Notifications\ResetPasswordと非常によく似ています。実際には、デフォルトのResetPasswordクラスから拡張することができます。

唯一の違いはここにある、あなたは、電子メールの件名を定義するための新しいメソッドの呼び出しを追加します。

return (new MailMessage) 
     ->subject('Your Reset Password Subject Here') 

あなたはMail Notifications hereについての詳細を読むことができます。

第2に、app\User.phpファイルでは、Illuminate\Auth\Passwords\CanResetPassword特性で定義されているデフォルトのsendPasswordResetNotification()メソッドをオーバーライドする必要があります。

<?php 

namespace App; 

use Illuminate\Notifications\Notifiable; 
use Illuminate\Foundation\Auth\User as Authenticatable; 
use App\Notifications\ResetPassword as ResetPasswordNotification; 

class User extends Authenticatable 
{ 
    use Notifiable; 

    ... 

    public function sendPasswordResetNotification($token) 
    { 
     // Your your own implementation. 
     $this->notify(new ResetPasswordNotification($token)); 
    } 
} 

そして今、あなたのパスワードリセットのメールの件名を更新する必要があります。今、あなたはあなた自身のResetPasswordの実装を使用する必要があります!

Reset password email subject updated

は、このヘルプを願っています!

+0

と、どのようにLaravelとLaravelについて考えてみましょうか? – Steve

+1

@Steve config/app.phpに移動し、アプリケーション名を変更してください – kniteli

1

を簡単にユーザーにパスワードリセットのリンクを送信するために使用される通知のクラスを変更することがあります。まず、UserモデルのsendPasswordResetNotificationメソッドをオーバーライドします。この方法では、選択した通知クラスを使用して通知を送信することができます。パスワード$tokenをリセットするにはこの方法で受信された最初の引数で、このことができますDoc for Customization

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

希望を参照してください!

+0

はるかに単純な受け入れられたもの..! –

2

このようなリセットパスワードトークンを作成するカスタム関数を作成することができます。

$user = User::where('email', '[email protected]')->first(); 
$password_broker = app(PasswordBroker::class); //so we can have dependency injection 
$token = $password_broker->createToken($user); //create reset password token 
$password_broker->emailResetLink($user, $token, function (Message $message) { 
     $message->subject('Custom Email title'); 
});//send email. 
0

だけの行を追加します。 - :

public function toMail($notifiable) 
{ 
    return (new MailMessage) 
     ->subject('New Subjetc') 
     ->line('You are receiving this email because we received a password reset request for your account.') 
     ->action('Restaurar Contraseña', url(config('app.url').route('password.reset', $this->token, false))) 
     ->line('If you did not request a password reset, no further action is required.'); 
} 

このようなファイルを照らし\認証\通知\ ResetPasswordの の方法toMailに>件名( '新Subjetc')

関連する問題