1. Front desk login
Use directly the auth that comes with laravel
php artisan make:auth
Then you can view the routing file:
Route::group(['middleware' => 'web'], function () { Route::auth(); Route::get('/home', 'HomeController@index'); });
Execute php artisan migrate
You will find that two tables are generated.
2. Background login
Edit configuration file
config\auth.php
Add admin in guards and admins in providers
<?php return [ 'defaults' => [ '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
Then modify database\migrations For the data table structure in, you can copy the user table
Execute php artisan migrate and you will find that the admin table is generated
4. Define the background route
Here I am Directly defines 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
Execute
php artisan make:controller Admin/AuthController php artisan make:controller Admin/AdminController
AuthController.php can refer to AuthController.php
<?php namespace App\Http\Controllers\Admin; use App\Admin; use Validator; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\ThrottlesLogins; use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; class AuthController extends Controller { use AuthenticatesAndRegistersUsers, ThrottlesLogins; protected $redirectTo = '/admin'; protected $guard = 'admin'; protected $loginView = 'admin.login'; protected $registerView = 'admin.register'; public function __construct() { $this->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
<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use Auth; class AdminController extends Controller { public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); } }
6 in Auth .Create view
Here directly copy the view in auth and home.blade.php to a new admin folder.
Modify the action of the form in login and register, add the admin prefix
Modify app.blade.php in layouts
<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/register') }}">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::guard('admin')->user()->name }} <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li><a href="{{ url('/admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li> </ul> </li> @endif </ul>
Now try to log in to the front and backend separately!
7. Possible page jump problems
If you encounter this situation, you can try to modify Middleware\Authenticate.php
return redirect()->guest($guard.'/login');
The above is the method that the editor introduces to you in laravel5.2 to realize the distinction between front and back user login. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!
For more related articles on laravel5.2's method of distinguishing between front and back user logins, please pay attention to the PHP Chinese website!