且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

OctoberCMS:如何检查用户是否登录了所有页面

更新时间:2023-09-10 20:33:16

您可以按照建议使用Session Component,但是您需要两种布局:

You can use the Session Component as suggested but then you need two layouts :

a)注册用户的布局>>将会话组件添加到此布局,并使用重定向选项设置security = "user"

a) Layout for registered Users >> Add session component to this layout and set security = "user" with the redirect option

b)公共页面的布局(未注册)>>添加另一个会话组件>> security = "guest"

b) Layout for public pages ( non-registered ) >> Add another session component >> security = "guest"

另一种选择是为这些路由创建中间件;

Another option could be to create a Middleware for those routes;

public function handle($request, Closure $next)
{


    App::before(function () {

        if (App::runningInBackend() || App::runningInConsole()) {
            return;
        }

        if ( !Auth::getUser() ) {

              return Redirect::to('/login');
        }


    });

    return $next($request);
}

保持简单,如果您正在使用RainLab插件,并且只需要检查用户是否已登录,会话组件就可以完成这项工作.

Keep it simple, if you're using the RainLab Plugin and just need to check a user is logged in, the session component would do the job.