1, first determine the Reset password routing
When we installed the Laravel, the default generated reset password was performed without the user logging in. So using the original controller is not feasible, and the original reset password, do not need to see the original password is correct, but through the mail to make a direct change of password, so the controller method, we also need to write a new one. We use php artisan make:controller UserController
to create a controller class and then create two routes Route::get('reset', 'UserController@getReset')
and Route::post('reset', 'UserController@postReset')
.
The former is a page get request that displays a reset password, followed by a reset password post request.
2. Display the Reset Password page
This is the getReset
method that only needs to display a view so there is no particular logic
Public Function Getreset ()
{return
view (' Auth.reset ');
}
3. Request Reset Password
This is the postReset
method used to receive data, we use two methods to receive data can be: one is to use the request method to receive data , the other is to use the Input::get method to obtain data . Request
If you need to introduce use Illuminate\Http\Request
classes, Input
then you need use Input
to introduce classes, where we choose request
to use to receive.
4. Validation rules
Verification, Laravel provides us with a set of validation rules, using validator
methods to Validator::make()
Validate
$data = $request->all (); Receive all data
$rules = [
' OldPassword ' => ' required|between:6,20 ', '
password ' => ' Required|between : 6,20|confirmed ',
];
$messages = [
' Required ' => ' password cannot be empty ',
' between ' => ' password must be between 6~20 bits ',
' confirmed ' => ' The new password and Confirm password do not match '
];
$validator = Validator::make ($data, $rules, $messages);
$data
Receive the data from the from;
rules
To evaluate the received value, in which the previous oldpassword
and the password
original password received from the front end and the name field data of the new password are validated;
Validation rules in the Manual of the verification section are, it is noteworthy that the use of confirmed words is for the new password and confirm the password to make the same judgment, confirm the password must be the name value must be the new password after the name value Plus ' _confirmation'
, such as the new password name value for newpassword
words, The name value of the confirmation password must be the newpassword_confirmation
only messages
data request that can be judged on the validation, and what prompts are displayed.
Then through the above verification, there is a case is not validated, that is, the original password entered is the same as the original password in the database.
Here we can first the user's information from the database to find out, and then enter the original password to compare. Here we use Auth::user()
to get the user's information, this method needs to introduce the use Auth;
class, and then pass Hash::check()
the password to judge. After judgment, there is a problem, that is, how to put the error message into the validator error message, here Laravel provide us with the After method:
$user = Auth::user ();
$validator->after (function ($validator) use ($oldpassword, $user) {
if!\hash::check ($oldpassword, $user-> Password) {//The original password and the password in the database are compared to
$validator->errors ()->add (' OldPassword ', ' original password error ');////Error display original password error
}
});
if ($validator->fails ()) { //To determine if there is an error
return back ()->witherrors ($validator);//redirect page, The error message is stored in a one-time
session
$user->password = Bcrypt ($password); Use the Bcrypt function to encrypt the new password
$user->save (); After successful, save the new password
Because after
of the introduction of a PHP anonymous function, we need to use the keyword to pass the use
external data to the anonymous function (ps:php new features, closures, and anonymous functions).
We introduced a global function in the anonymous function, so we need to add \ (ps:php new features, namespace chapters, global namespaces) to the function.
5, Front end display error message
The front-end display, we use $errors
variables to display errors, according to the official documentation, the call is Illuminate\Support\MessageBag
the example, interested, you can look. We use count($errors) > 0
to determine if there are errors, using $errors->first()
An error message that displays:
@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
Some people may ask if my error is not displayed in a fixed place, but instead displays the error message after each form, so how do we judge and display it? The answer is $errors->has('oldpassword')
to use to determine if there is an error with this name, and if so, use $errors->first('oldpassword')
the error shown:
@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
oldpassword
This is the name value in each form, so when you add a after
custom error using the method, be $validator->errors()->add('oldpassword', '原密码错误');
sure to oldpassword
write a correct error in which form you want to display correctly.
6, after the completion of the example
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|between:6,20 ', ' Password ' => ' required|between:6,20|confirmed ',];
$messages = [' Required ' => ' password cannot be empty ', ' between ' => ' password must be between 6~20 bits ', ' confirmed ' => ' new password and Confirm password mismatch '];
$validator = Validator::make ($data, $rules, $messages);
$user = Auth::user (); $validator->after (function ($validator) use ($oldpassword, $user) {if!\hash::check ($oldpassword, $user->PASSW
ORD)) {$validator->errors ()->add (' OldPassword ', ' original password error ');
}
}); if ($validator->fails ()) {return back ()->witherrors ($validator);//Returns a one-time error} $user->password = Bcrypt ($p
Assword);
$user->save (); Auth::logout ();
After changing this password, exit this user return redirect ('/login '); }
Reset.blade
<form class= "Login-form" action= "method=" POST ">
Summarize
The above is the entire content of this article, I hope that you learn to use Laravel help, if there are questions to welcome the message discussion.
Original: Dennis ' s blog