不同功能的构造器会话验证 [英] Constructor session validation for different functions

查看:127
本文介绍了不同功能的构造器会话验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的构造函数中进行验证,该构造函数检查用户是否已启动会话。

I'm doing a validation in my constructor which checks whether the user has started a session.

然后我会为各种东西投入一些控制器。

I'm then throwing in some controllers for various things.

我的问题是 - 的控制器不受构造函数(会话验证)的影响,或者我必须把它带到另一个文件?原因是,我想在构造函数中有两组函数 - 一个用于用户登录时,其余的则不在。

My question is - can I have some of the controllers unaffected by the construct function(session validation), or do i have to take it into another file? The reason is, that I would like to have two sets of function in the constructor - one for when the user is logged in, and the rest when they are not.

感谢

推荐答案

您需要做的是设置一个基础控制器,它将为您保存会话,并通过继承将登录的控制器与您注销的控制器分开。

What you need to do is set up a base controller that will look after the session for you and split your logged in controllers from your logged out ones via inheritance.

我的建议是查看Phil Sturgeon在保持干燥。在他的帖子中,Phil解释了如何实现控制器的基础知识,使父控制器会关注会话,所以你不必每次都检查它。

My suggestion for you is to have a look at Phil Sturgeon's post on Keeping It Dry. In his post, Phil explains the basics on how to implement controllers in such a way that the parent controller will look after sessions so you don't have to check it every time.

作为示例:

class MY_Controller extends CI_Controller{
    function __construct(){
        parent::__construct();
        //do things in here that ALL controllers should do.
    }
}



MY_In_Controller:



MY_In_Controller:

class MY_In_Controller extends MY_Controller{
    function __construct(){
        parent::__construct();
        //if user doesnt have the session redirect them out NOW!
    }
}



MY_Out_Controller:



MY_Out_Controller:

class MY_Out_Controller extends MY_Controller{
    function __construct(){
        parent::__construct();
        //if the user has the session, redirect them in NOW.
    }
}



登录控制器:



Login Controller:

class Login extends MY_Out_Controller{
    function __construct(){
        parent::__construct();
    }

    function index(){
        //folks that have a session (already logged in) will never even get here.
        //because the parent class MY_Out_Controller already redirected them back in.
    }
}


b $ b

管理控制器:



Manage Controller:

class Manage extends MY_In_Controller{
    function __construct(){
        parent::__construct();
    }

    function index(){
        //folks that don't a session (logged out) will never even get here.
        //because the parent class MY_In_Controller already redirected them out.
    }
}

总之,不要直接写会话检查在控制器。您会太频繁地写信,违反干燥原则

In short, don't write your session checks directly in controllers. You'll be writing it too often and violating the DRY Principle.

更新:我建议您使用Shane Pearson的方法修改Phil的方法,方法在重读CodeIgniter的基本类

Update: I recommend revamping Phil's method by using Shane Pearson's method in CodeIgniter's Base Classes Revisited.

这篇关于不同功能的构造器会话验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆