Laravel 5.3+ Reset Password message style and content modification (use of notifications)

Source: Internet
Author: User
Tags auth mailmessage

Laravel we can use PHP artisan Make:auth to generate a set of default login reset mailbox authentication system, but how to modify the system sent to the user's reset password message style and content.

Although the default message style is very beautiful, but unavoidably all is in English, we can at least add some Chinese hints to facilitate the user to view.

First of all we need to be clear: Laravel default Notification class is ResetPassword, located in Illumintate/auth/notifications. We should not directly modify the code located in ResetPassword because updating package may result in overwriting.

Let's take a look at ResetPassword:

<?php
namespace Illuminate\auth\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)
            ->line (' Your are receiving this email Because we received a password reset request for your account ')
            ->action (' Reset password ', url (config (' App.url '). Route (' Password.reset ', $this->token, False))
            ->line (' If you did not request a password reset, no further ACTI On is required; '
    }
}

As you can see, ResetPassword expands the notification class, so what we need to do is create a new notification class to complete our custom email content modifications:

$ php Artisan make:notification resetpasswordnotification

Enter the above artisan command and we will find that in app\ Notifications folder under a file called resetpasswordnotification.php, open it, we can see that its content and resetpassword very similar. All we need to do is modify the key code:

<?php namespace Illuminate\auth\notifications;
Use illuminate\bus\queueable;
Use illuminate\notifications\notification;
Use Illuminate\contracts\queue\shouldqueue;

Use Illuminate\notifications\messages\mailmessage;

    Class Resetpasswordnotification extends Notification {use queueable;

    Public $token;
    Public function __construct ($token) {$this->token = $token;
    Public function via ($notifiable) {return [' Mail '];
            Public Function Tomail ($notifiable) {return (new MailMessage)->line (' Here's what we need to add ')
            ->line (' You are receiving the email because we received a password reset request for your account ') ->action (' Reset Password ', url (config (' App.url '). Route (' Password.reset ', $this->token, false))-& Gt;line (' Here's what we need to add ')->line (' If You're did not request a password reset, no further the action is required. ')
    ); }
}

As you can see, we can use line as the unit to add the information we need.

So the information content is done, how to modify the message style. First we need a blade template that can modify information:

$ php Artisan vendor:publish--tag=laravel-notifications

The above command publishes the template in the package to the Resources/views/vendor/notifications folder so that we only need to modify the resources/views/vendor/notifications/ Email.blade.php is OK.

In the final step, we add the user model:

/**
 * Send the password reset notification.
 * *
 @param  string  $token
 * @return void
 *
/Public Function sendpasswordresetnotification ($ Token)
{
    $this->notify (New Resetpasswordnotification ($token));
}

In this way, we can use Resetpasswordnotification to send the message.

Template modification is very simple, here is not to repeat, when finished, we can see the new message content:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.