If you need to use Laravel to build a back-end content management system, but laravel default login registration can not meet the current needs, reset password because it is used in the background, and do not need to send a message to reset, so the default reset password is definitely not. Follow this article to see how to perform a laravel reset password refactoring.
1. First determine the route to reset the password
When we install the Laravel, the default reset password is generated when the user is not logged 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 again. We used 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 method is used getReset
, 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 is the method used to postReset
receive the data, we use two methods to receive the data transmitted: 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
you need to introduce use Input
classes, and here we choose request
to use them to receive.
4. Validation rules
Validation, Laravel provides us with a set of validation rules that are validated using validator
the Validator::make()
method
$data = $request->all (); Receive all data $rules = [ ' oldpassword ' = ' required|between:6,20 ', ' password ' = ' required|between:6,20| ' Confirmed ',]; $messages = [ ' Required ' = ' password cannot be null ', ' between ' = ' password must be between 6~20 bits ', ' confirmed ' = > ' New password and confirmation password do not match '; $validator = Validator::make ($data, $rules, $messages);
$data
Receiving the data from the from;
rules
The value of the docking received is judged, where the preceding array oldpassword
and the password
Name field data from the original and new password received from the front end are validated;
Validation rules are in the manual validation section, it is worth noting that the use of confirmed is for the new password and confirm the password for the same judgment, the confirmation password must be the name value must be the new password after the name value, _confirmation'
such as the name value of the new password, newpassword
The name value of the confirmation password must be for the newpassword_confirmation
data request to be judged for messages
validation and what prompt is displayed.
Then through the above verification, there is another case is not verified, 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 the input of 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 through Hash::check()
the password to judge. After judging, there is a problem, that is, how to press the error message into the validator error message, here Laravel provides us with after method:
$user = Auth::user (); $validator->after (function ($validator) use ($oldpassword, $user) { if (!\hash::check ($ OldPassword, $user->password)) {//original password and password in database $validator->errors ()->add (' OldPassword ', ' original password error ') ; Incorrect display of the original password error }}); if ($validator->fails ()) { //To determine if there is an error return back ()->witherrors ($validator); /redirect the page and save the error message in a single session} $user->password = Bcrypt ($password); Use the Bcrypt function to encrypt the new password $user->save (); After success, save the new password
Because after
of the introduction of an anonymous PHP 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)
In the anonymous function we introduce a global function so we need to add \ (ps:php new feature, namespace chapter, global namespace) in front of the function
5, front-end display error message
The front-end display, we use $errors
variables to display errors, according to official documentation, the invocation is an example of an Illuminate\Support\MessageBag
interest that can be seen below. We use it count($errors) > 0
to determine if there is an error, using the $errors->first()
display of an error message:
@if (Count ($errors) > 0) <p class= "alert Alert-danger display-hide" style= "Display:block;" > <button class= "Close" data-close= "alert" ></button> <span> </span> </p> @endif
Someone might ask, if my error is not displayed in a fixed place, but instead shows the error message after each form, so how can 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 to $errors->first('oldpassword')
display this error:
@if ($errors->has (' OldPassword ')) <p class= "alert Alert-danger display-hide" style= "Display:block;" > <button class= "Close" data-close= "alert" ></button> <span> </span> </p> @endif
oldpassword
This is the name value in each form, so after
when using the method to add custom errors $validator->errors()->add('oldpassword', '原密码错误');
, be sure to write the error on oldpassword
which form is in order 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 null ', ' 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, Password) {$validator->errors ()->add (' OldPassword ', ' original password error '); } }); if ($validator->fails ()) {return back ()->witherrors ($validator);//Return one-time error} $user->password = Bcrypt ($pass Word); $user->save (); Auth::logout (); After changing this password, exit this user return redirect ('/login ');}
Reset.blade
<form class= "Login-form" action= "method=" POST ">
Related recommendations:
The eloquent relationship of learning Laravel5
Laravel writing the App interface (API)
The problem and solution of laravel5.5 Controller's parameter sequence