天天看點

建站篇-使用者認證系統-管理者登陸背景

計劃将使用者都存放在users表中,依靠role判斷是否可以登陸管理背景。

對應的我們需要建立Role.php在App\Model下(暫時不用管其中的permissions方法)

對應的User.php中加上方法

public functionroles(){     return    $this->belongsToMany('App\Model\Role','role_user','user_id','role_id'); }

role_user為他們的關聯表,隻有role_id 和 user_id兩個字段

首先建立登入頁面auth/admin/login.blade.php

和使用者登入界面類似,代碼不再重複。注意Post路由改一下。

添加路由到web.php

Route::group(['prefix' => 'admin'], function () {     Route::get('login', 'Admin\Auth\LoginController@showLoginForm'); });

完成showLoginForm代碼

添加guest檢測

其中中間件guest.backend 為'guest.backend'=>\App\Http\Middleware\Auth\RedirectIfAuthenticatedBackendUser::class,

同時完成login方法

其中使用到了RoleService,檔案建立在APP\Services下

擁有登入背景權限的role角色記錄在config檔案Role.php中

'backend'=>[      'admin', ],

注意到,登入成功後跳轉到

protected    $redirectTo='admin/index';

完成index方法在IndexController中

中間件role.backend.access為

'role.backend.access'=>\App\Http\Middleware\Role\BackendAuthenticated::class,