OctoberCMS:如何检查用户是否登录了所有页面 [英] OctoberCMS: How to check if user logged in for all pages
问题描述
我正在使用以下代码(在页面的代码部分)进行重定向用户未登录的情况下登录到登录页面:
I am using the following code (in the code section of a page) to redirect users to the login page if they are not logged in:
function onStart()
{
$user = $this->account->user();
if(!$user)
{
return Redirect::to('/login');
}
}
如何避免在每个页面中都执行此操作,而对需要身份验证的所有页面执行此检查呢?
How can I avoid doing this in every page and perform this checking in one place for all the pages that need authentication?
推荐答案
您可以按照建议使用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.
这篇关于OctoberCMS:如何检查用户是否登录了所有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!