This article mainly introduces laravel5.2's method for distinguishing between frontend and backend user logon. it is very good and has reference value. if you need it, refer to section 1. frontend logon.
Directly use the auth provided by laravel
php artisan make:auth
Then you can view the route file:
Route::group(['middleware' => 'web'], function () { Route::auth(); Route::get('/home', 'HomeController@index');});
Execute php artisan migrate
Two tables are generated.
2. background logon
Edit configuration file
Config \ auth. php
Add admin in guards and admins in providers
[ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ],];
3. create a data model
Execute php artisan make: model Admin -- migration
Modify the data table structure in database \ migrations and copy the user table.
Execute php artisan migrate and you will find that the admin table is generated.
4. define backend routes
Here I have defined a routing group.
Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index');});
5. create a controller
Run
php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController
For AuthController. php, refer to AuthController. php in Auth.
middleware('guest:admin', ['except' => 'logout']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}
AdminController. php
middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); }}
6. create a view
Copy the view and home. blade. php in auth to a new admin folder.
Modify the action of the form in login and register, and add the admin prefix.
Modify app. blade. php in layouts
@if (Auth::guest('admin'))
- Login
- Register
@else
- {{ Auth::guard('admin')->user()->name }}
@endif
Now, try logging on to the front and back ends separately!
7. possible page jump problems
If this happens, try to modify Middleware \ Authenticate. php.
return redirect()->guest($guard.'/login');
The above section describes how laravel5.2 distinguishes between frontend and backend user logon. I hope it will be helpful to you. if you have any questions, please leave a message and I will reply to you in a timely manner. I would like to thank you for your support for PHP chinnet!
For more information about how laravel5.2 can distinguish between frontend and backend user logon methods, refer to PHP!