1.前台登錄
直接使用laravel自帶的auth
php artisan make:auth
然後可以查看路由文件:
Route::group(['middleware'=>'web'],function() {
Route::auth();
Route::get('/home','HomeController@index');
});
執行php artisan migrate
會發現生成了兩張表。
2.後台登錄
編輯配置文件
config\auth.php
添加guards中的admin和providers中的admins
<?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.創建數據模型
執行php artisan make:model Admin --migration
然後修改database\migrations中的數據表結構,復制user表的就可以
執行php artisan migrate 會發現生成了admin表
4.定義後台路由
這裡我直接定義了一個路由組
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.創建控制器
執行
php artisan make:controller Admin/AuthController
php artisan make:controller Admin/AdminController
AuthController.php可參照Auth中的AuthController.php
<?php
namespaceApp\Http\Controllers\Admin;
useApp\Admin;
useValidator;
useApp\Http\Controllers\Controller;
useIlluminate\Foundation\Auth\ThrottlesLogins;
useIlluminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
classAuthControllerextendsController
{
useAuthenticatesAndRegistersUsers, ThrottlesLogins;
protected$redirectTo='/admin'
protected$guard='admin'
protected$loginView='admin.login'
protected$registerView='admin.register'
publicfunction__construct()
{
$this->middleware('guest:admin', ['except'=>'logout']);
}
protectedfunctionvalidator(array$data)
{
returnValidator::make($data, [
'name'=>'required|max:255',
'email'=>'required|email|max:255|unique:admins',
'password'=>'required|confirmed|min:6',
]);
}
protectedfunctioncreate(array$data)
{
returnAdmin::create([
'name'=>$data['name'],
'email'=>$data['email'],
'password'=> bcrypt($data['password']),
]);
}
}
AdminController.php
<?php
namespaceApp\Http\Controllers\Admin;
useIlluminate\Http\Request;
useApp\Http\Requests;
useApp\Http\Controllers\Controller;
useAuth;
classAdminControllerextendsController
{
publicfunction__construct()
{
$this->middleware('auth:admin');
}
publicfunctionindex()
{
$admin= Auth::guard('admin')->user();
returnview('admin.home');
}
}
6.創建視圖
這裡直接復制auth中的視圖和home.blade.php到一個新的admin文件夾中。
修改login和register中表單的action,添加上admin前綴
修改layouts中的app.blade.php
@if(Auth::guest('admin'))
Login
Register
@else
{{ Auth::guard('admin')->user()->name }}
@endif
現在試一下前後台分別登錄吧!
7.可能遇到的頁面跳轉問題
如果你遇到了這種情況可以嘗試修改一下Middleware\Authenticate.php
returnredirect()->guest($guard.'/login');