Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

codeigniter - Constructor session validation for different functions

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.

I used to have two files and switch between them, but then i decided to combine them into one.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

As an example:

MY_Controller:

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

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:

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.
    }
}

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...