Implementation of laravel password reset in PHP

Source: Internet
Author: User
Tags php server
This time to bring you the implementation of PHP laravel password reset, php laravel password reset the attention to what, the following is the actual case, together to see.

Brief introduction

Want to quickly implement this feature? Just run under the newly installed Laravel app php artisan make:auth (if you've already executed this command, you can ignore it), and then access Http://your-app.dev/register or other URLs assigned to the app in your browser. This command generates everything a user needs to sign in to register, including password reset!

Most Web apps provide the ability to reset passwords for users, and Laravel is no exception, Laravel provides a convenient way to send password reset links and implement password reset logic without having to repeat the implementation yourself in each app.

Note: The User model must use the illuminate\notifications\notifiable trait before using the password reset feature provided by Laravel.

Database related

Before you begin, verify that the App\user model implements the Illuminate\Contracts\Auth\CanResetPassword contract. Of course, Laravel's own App\user model has implemented the interface and uses Illuminate\auth\passwords\canresetpassword trait to contain the methods needed to implement the interface.

Generate a Reset token table migration

Next, the table used to store the password reset token must be created, and Laravel has already migrated the table, which is stored in the Database/migrations directory. So all you have to do is run the migration:

php artisan migrate

This table is password_resets:

Routing

The Laravel comes with Auth\ForgotPasswordController the Auth\ResetPasswordController controller (these two controller classes are automatically generated via the PHP Artisan make:auth command), respectively, for sending password reset link messages and resetting the user password feature. The route required to reset the password has been automatically generated by the Make:auth command:

php artisan make:auth

The corresponding route definition is in the Auth method of Illuminate\routing\router:

View

As with routing, the view files required to reset the password are also generated by the Make:auth command, which is located in the

resources/views/auth/passwords directory, you can modify the generated files as needed.

Reset Password

Once you have defined the Reset user password route and view, you only need to access the portal route through/password/reset in the browser. The framework comes with the ForgotPasswordController logic to send a password reset linked message, including the ResetPasswordController logic to reset the user's password:

Enter the registration email and click the Send password reset link to send a password reset link to the mailbox:

Opening a mailbox will receive a password reset message:

Click the Reset Password button to go to the Reset Password page:

After completing the form submission, you can reset your password.

After the password is reset, the user will automatically log on to the app and redirect to/home. You can customize the jump link after a successful password reset by defining the Redirectto property of Resetpasswordcontroller:

protected $redirectTo = '/dashboard';

Note: By default, the password reset token is valid for an hour, and you can change the effective time by modifying the option expire in the config/auth.php file.

Custom

Custom Authentication Guard

In profile auth.php, you can configure multiple "guards" to implement standalone authentication based on multi-user tables, and you can use the Guard method on the built-in Resetpasswordcontroller controller to The method will return a guard instance:

Use illuminate\support\facades\auth;protected function Guard () {return Auth::guard (' Guard-name ');}

Custom Password Broker

In profile auth.php, you can configure multiple passwords so that you can use password Broker to reset multiple user tables, as well, by overriding the Forgotpasswordcontroller in your own and resetpasswordcontroller controllers. Broker method to use the broker of your choice:

Use Illuminate\support\facades\password;

/** * Gets the broker used during the password reset. * * @return Passwordbroker * @translator laravelacademy.org */protected function broker () {  return Password::broker (' Name ');}

Custom password Reset messages

You can easily edit the Send password reset link to the user's notification class to implement a custom password reset message, to implement this function, you need to override the method on the user model, sendPasswordResetNotification in this method, you can use any of your favorite notification class to send a notification, the method receives the first parameter is password reset $ Token:

/** * Send password reset notifications. * * @param string $token * @return void */public function Sendpasswordresetnotification ($token) {  $this->notify ( New Resetpasswordnotification ($token));}

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Custom two-dimensional array sorting function array

PHP server-side API and interface development detailed

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.