在登录模块中,会用到路由中间件,关于路由中间件的使用,在此我将利用实例说明:

    <?php
    // +———————————————————————————————————
    // | ThinkPHP [ WE CAN DO IT JUST THINK ]
    // +———————————————————————————————————
    // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
    // +———————————————————————————————————
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +———————————————————————————————————
    // | Author: liu21st liu21st@gmail.com
    // +———————————————————————————————————
    use think\facade\Route;
    Route::get(‘think’, function () {
    return’hello,ThinkPHP6!’;
    });
    Route::get(‘hello/:name’, ‘index/hello’);
    //后台组
    Route::group(function(){
    //用户模块路由
    Route::resource(‘user’,’User’);
    //权限模块路由
    Route::resource(‘auth’,’Auth’);
    })->middleware(function($request,Closure$next){
    if(!session(‘?admin’)){
    returnredirect(‘/login’);
    }
    return$next($request);
    });
    //登录路由
    Route::group(function(){
    Route::get(‘login’,’Login/index’);
    Route::post(‘login_check’,’Login/check’);
    });

    在上述实例代码中,对于(!session(‘?admin’))的解释如下:

    为了防止用户跳过登录页面,直接进入后台首页,因此需要一个关于session的判断。

    if(!session(‘?admin’)),其中admin是管理员表中的超级管理员名,其他管理员都不能满足这个条件。

    参考资料:
    https://blog.csdn.net/blbyu/article/details/119647628