Detailed examples Laravel password reset code reconstruction, examples laravel
1. First, confirm the route for resetting the password.
When laravel is installed, the Reset Password generated by default is performed without logon. Therefore, using the original controller is not feasible, and the original password reset does not need to check whether the original password is correct, but directly change the password through email, we also need to write a new one. We usephp artisan make:controller UserController
Create a controller class and then create two routesRoute::get('reset', 'UserController@getReset')
AndRoute::post('reset', 'UserController@postReset')
.
The former shows a page get request for password reset, followed by a password reset post request.
2. The Reset Password page is displayed.
This isgetReset
This method only needs to display a view, so there is no special logic
public function getReset(){ return view('auth.reset');}
3. Request Password Reset
This ispostReset
In this method, we can use either of the following methods to receive data:One is to use the request Method to receive data.,Another method is to use Input: get to obtain data..Request
You need to introduceuse Illuminate\Http\Request
Class,Input
You need to introduceuse Input
Class, here we choose to userequest
.
4. verification rules
For verification, laravel provides us with a set of verification rules, usingvalidator
OfValidator::make()
Method Verification
$ Data = $ request-> all (); // receives all data $ rules = ['oldpassword' => 'required | between: 6, 20 ', 'Password' => 'required | between: 6, 20 | confirmed ',]; $ messages = ['requestred' => 'password cannot be blank ', 'between' => 'the password must be 6 ~ Between 20 bits, 'confirmed '=> 'new password does not match confirm password']; $ validator = Validator: make ($ data, $ rules, $ messages );
$data
Received data information from;
rules
Judge the received value.oldpassword
Andpassword
Is to verify the data in the name field of the original password and new password received from the front end;
The verification rules are included in the Verification Section of the manual. It is worth noting that confirmed is used to make the same judgment for the new password and the confirmed password, confirm that the password must have a name value followed by the name value of the new password'_confirmation'
For example, the name of the new password isnewpassword
The password name must benewpassword_confirmation
Can be judgedmessages
What prompt is displayed for the data request for verification.
After the above verification, there is another situation that is not verified, that is, whether the entered original password is the same as the original password in the database.
Here, we can first find out the user information from the database, and then compare it with the entered original password. Here we useAuth::user()
To obtain user information, this method needs to be introduceduse Auth;
Class, and thenHash::check()
To determine the password. There is another problem after the judgment, that is, how to push the error information to the error message of validator. laravel provides the after method for us:
$ User = Auth: user (); $ validator-> after (function ($ validator) use ($ oldpassword, $ user) {if (! \ Hash: check ($ oldpassword, $ user-> password) {// compare the original password with the password in the database $ validator-> errors () -> add ('oldpassword', 'original password error'); // The original password is incorrectly displayed.}); if ($ validator-> fails ()) {// determine whether an error is returned back ()-> withErrors ($ validator); // redirect the page, and save the error information to the one-time session} $ user-> password = bcrypt ($ password); // use the bcrypt function to encrypt the new password $ user-> save (); // Save the new password
This is becauseafter
A PHP anonymous function is introduced, so we need to useuse
Keywords: pass external data to anonymous functions (PS: New Features of php, closures and anonymous functions)
We have introduced a global function in the anonymous function, so we need to add \ (PS: New php features, namespace chapter, global namespace) before the function)
5. Front-end error messages
For front-end display, we use$errors
Variable to display the error. According to the official documentation, the call isIlluminate\Support\MessageBag
For more information, see. We usecount($errors) > 0
To determine whether there is an error. $errors->first()
An error message is displayed:
@if(count($errors) > 0) <div class="alert alert-danger display-hide" style="display: block;"> <button class="close" data-close="alert"></button> <span> </span> </div>@endif
Someone may ask, if my error is not displayed in a fixed place, but an error message is displayed behind each form, how can we judge and display it? The answer is to use$errors->has('oldpassword')
To determine whether this name is correct. If yes, use$errors->first('oldpassword')
This error is displayed:
@if( $errors->has('oldpassword') ) <div class="alert alert-danger display-hide" style="display: block;"> <button class="close" data-close="alert"></button> <span> </span> </div>@endif
Whereoldpassword
Is the name value in each form, soafter
When a custom method is added$ Validator-> errors ()-> add ('oldpassword', 'incorrect original password ');
Medium,oldpassword
Make sure that the correct form is incorrect so that it can be correctly displayed.
6. example after completion
UserController
Public function getReset () {return view ('auth. reset');} public function postReset (Request $ request) {$ oldpassword = $ request-> input ('oldpassword '); $ password = $ request-> input ('Password'); $ data = $ request-> all (); $ rules = ['oldpassword' => 'required |: 6, 20 ', 'Password' => 'required | between: 6, 20 | confirmed',]; $ messages = ['requestred' => 'password cannot be blank ', 'between' => 'the password must be 6 ~ Between 20 bits, 'confirmed '=> 'new password does not match confirm password']; $ validator = Validator: make ($ data, $ rules, $ messages ); $ user = Auth: user (); $ validator-> after (function ($ validator) use ($ oldpassword, $ user) {if (! \ Hash: check ($ oldpassword, $ user-> password) {$ validator-> errors ()-> add ('oldpassword ', 'original password error') ;}}); if ($ validator-> fails () {return back ()-> withErrors ($ validator ); // return a one-time error} $ user-> password = bcrypt ($ password); $ user-> save (); Auth: logout (); // after changing the password, exit the user return redirect ('/login ');}
Reset. blade
<Form class = "login-form" action = "" method = "post">
Summary
The above is all the content in this article. I hope it will help you learn how to use Laravel. If you have any questions, please leave a message to discuss it.
Original article: Dennis's blog