建站篇-用户认证系统-管理员登陆后台

计划将用户都存放在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,


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容