Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Registration refactoring in Laravel

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of registration refactoring in Laravel, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

1. First determine the route registered by the user

When we install laravel, the registration generated by default is registered by mailbox, and some options are not needed, and some need to add some form options.

If we register, we can not register casually, only some super administrators can register.

First, we use the UserController we created last time for configuration. If not, we can use php artisan make:controller UserController to create a controller class.

Then create two routes Route::get ('register',' UserController@getRegister') and Route::post ('register',' UserController@postRegister')

The former displays a get request for a registered page, followed by a post request for a registered account.

two。 Display the registered account page

This uses the getRegister method, which only needs to display a view, so there is no special logic.

Public function getRegister () {return view ('auth.register');}

3. Request to register an account

This one uses the postRegister method.

Registering an account is the same as resetting your password, and it's a little easier than registering an account.

When we insert a user record into the database, we can insert it using User::create ($data).

$data is an array containing the keys and values of each field

Public function postRegister (Request $request) {$rules = ['username'= >' required | unique:finance_enewsuser', 'password' = >' required | between:6,20 | confirmed']; $messages = ['required'= >': attribute cannot be empty', 'unique'= >' username has been registered', 'between' = >' password must be between 6'20 digits', 'confirmed' = >' new password and confirm password do not match']; $username = $request- > input ('username'); $password = $request- > input (' password') $group = $request- > input ('group'); $data = $request- > all (); $validator = Validator::make ($data, $rules, $messages); if ($validator- > fails ()) {return back ()-> withErrors ($validator) } $data = ['username' = > $username,' password' = > bcrypt ($password), 'groupid' = > $group,' checked' = > 0, 'styleid' = > 1,' filelevel' = > 0, 'loginnum' = > 0,' lasttime' = > time (), 'lastip' = >' 127.0.0.1, 'truename' = >', 'email' = >', 'pretime' = > time (),' preip' = > '127.0.1),]; User::create ($data) / / insert a new record and return the saved model instance / / if you want to log in immediately after registration, you can use $user = User::create ($data); Auth::login ($user); authenticate return redirect ('/');}

4. Example after completion

UserController

Public function getRegister () {return view ('auth.register');} public function postRegister (Request $request) {$rules = [' username'= > 'required | unique:finance_enewsuser',' password' = > 'required | between:6,20 | confirmed']; $messages = [' required'= >': attribute cannot be empty', 'unique'= >' username has been registered', 'between' = >' password must be between 6'20 digits', 'confirmed' = >' new password and confirm password do not match'] $username = $request- > input ('username'); $password = $request- > input (' password'); $group = $request- > input ('group'); $data = $request- > all (); $validator = Validator::make ($data, $rules, $messages); if ($validator- > fails ()) {return back ()-> withErrors ($validator) } $data = ['username' = > $username,' password' = > bcrypt ($password), 'groupid' = > $group,' checked' = > 0, 'styleid' = > 1,' filelevel' = > 0, 'loginnum' = > 0,' lasttime' = > time (), 'lastip' = >' 127.0.0.1, 'truename' = >', 'email' = >', 'pretime' = > time () 'preip' = >' 127.0.0.1,] User::create ($data); / / insert a new record and return the saved model instance return redirect ('/');}

Register.blade

{!! Csrf_field ()!!} Sign Up @ if (count ($errors) > 0) @ endif username password duplicate password user group Super Admin Edit Registration

5. Middleware-user must log in

Now that the registration is complete, all we need is the user's judgment. The requirement registration account must be an account with Super Admin privileges to register.

In this case, our general step is to find out the user's information directly in the postRegister method, and then check whether the user meets this permission, and then jump to another page if not.

This method is fine, but since we have the distinction between super admins and administrators, it must be used in more than one place, but also in other places.

Then someone will think of writing a method in model, which can be called directly later if necessary.

This method is fine, but we recommend using the middleware feature provided by laravel, which is very powerful and easy to use. Now we will use the middleware function.

Because we are the background content management system, so, we first create a middleware, the function is that all pages before entering, must be login status, otherwise jump to the login page.

Looking at the manual found that you can use the php artisan make:middleware CheckLoginMiddleware command to create a middleware, of course, copy a similar file, change is the same.

Then we will create a CheckLoginMiddleware middleware file in the app/Http/Middleware/ directory with only one handle () method, in which we will add our function directly.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report