Forgetting passwords is one of the most common scenarios in an application, and Laravel 5.1 also provides support for password resetting, which is easy to do with a little configuration.

1. Implementation ideas

The password is reset by sending a reset password link containing a specific token to the user’s registered mailbox, and then the user logs in to the mailbox by accessing the reset password link.

2. Data sheets & models

  • implementationCanResetPasswordContractContract and useCanResetPasswordThe traitUserModel (Laravel)
  • A table for storing reset password tokenspassword_resets(Laravel comes with the table corresponding to the migration file,In the previous sectionHas been created with the

3. Create a route

Laravel comes with a controller for password reset, Auth\PasswordController, in the same directory as the AuthController mentioned in the previous section. The business logic associated with resetting passwords is implemented through the ResetsPasswordstrait used in this controller. Here we define routing rules for resetting passwords in routes.php:

// Send password reset link Route::get('password/email'.'Auth\PasswordController@getEmail');
Route::post('password/email'.'Auth\PasswordController@postEmail'); // Password reset Route Route::get('password/reset/{token}'.'Auth\PasswordController@getReset');
Route::post('password/reset'.'Auth\PasswordController@postReset');Copy the code

4. Create a view

Defined routing after we get requests for corresponding view definition file, send the password reset link routing first create corresponding view resources/views/auth password. Blade. PHP:

<form method="POST" action="/password/email"> {!!!!! csrf_field() !! } <div> Email <inputtype="email" name="email" value="{{ old('email') }}">
    </div>

    <div>
        <button type="submit"</button> </div> </form>Copy the code

Then create a password reset routing corresponding view resources/views/auth/reset. Blade. PHP:

<form method="POST" action="/password/reset"> {!!!!! csrf_field() !! } <inputtype="hidden" name="token" value="{{ $token }}"> <div> Email: <inputtype="email" name="email" value="{{ old('email') }}"<div> <div> New password: <inputtype="password" name="password"> </div> <div> Confirm password: <inputtype="password" name="password_confirmation">
    </div>

    <div>
        <button type="submit"</button> </div> </form>Copy the code

In addition we need to create an additional view – send a password reset link mail template view resources/views/emails/password. The blade. PHP, is used to provide a view template for this email:

Click here to reset password: {{url('password/reset/'.$token)}}Copy the code

If the mail template view file path is located elsewhere, don’t forget to configure the password.email value in config/auth.php to correspond to the new path.

5. Configure email sending

The next thing we need to do is configure the relevant files to implement the email sending function in preparation for the next test.

Laravel uses the mail API provided by SwiftMailer library to implement mail operation. For details, please refer to the mail document. Here we only make simple configuration to implement mail sending, and the mail configuration file is config/mail.php:

<? phpreturn [
    'driver' => env('MAIL_DRIVER'.'smtp'),
    'host' => env('MAIL_HOST'.'smtp.mailgun.org'),
    'port' => env('MAIL_PORT', 587),
    'from'= > ['address' => null, 'name' => null],
    'encryption' => env('MAIL_ENCRYPTION'.'tls'),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'sendmail'= >'/usr/sbin/sendmail -bs'.'pretend'= >false,];Copy the code

As you can see, most of the configuration is set in the. Env file, where my.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=25
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=null
Copy the code

I use email 163. For other email addresses, refer to the corresponding email Settings and fill in my account information to MAIL_USERNAME and MAIL_PASSWORD.

We also need to configure the from configuration in mail.php as follows:

'from'= > ['address'= >'[email protected] '.'name'= >'Laravel college'].Copy the code

You just need to match address with MAIL_USERNAME in the. Env file. The name value is the sender name in the mailbox, which can be customized.

With this step configured, it’s time to test the password reset.