如何保证用户登录后才可以访问控制器的方法?

用户系统 使用文档
用户系统 使用文档

当前 Controller 实现 Module\Member\Support\MemberLoginCheck 接口,同时在 Route 中使用中间件 WebAuthMiddleware,那该 Controller 中的所有方法都会进行登录检查,没有登录会自动跳转到 /login 进行登录。

如果需要排除某些方法登录,可以在 Controller 增加一个变量来过滤一些方法,添加进去的方法会忽略登录检查。

use Module\Member\Support\MemberLoginCheck;

class IndexController extends Controller implements MemberLoginCheck
{
    public static $memberLoginCheckIgnores = [
        'foo', 
    ];
    
    public function foo(){
        // 不需要登录
    }
    
    public function bar(){
        // 会登录检查
    }
    
    public function baz(){
        // 会登录检查
    }
}
更新: 2025-03-28 13:44:50
QQ
微信
客服