1

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

Ando
  • 1,802
  • 4
  • 25
  • 47

2 Answers2

3

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.

Jordan Arsenault
  • 7,100
  • 8
  • 53
  • 96
  • thank you this was very informative. Now I feel that currently I'm doing session verifications like a caveman.. – Ando Sep 19 '12 at 00:36
0

As stated earlier why don't you use separate different base controllers for specific purpose.

I would suggest as this way for you

let us take two part one let's say frontend controller which donot require session and another controller let's say backend controller which needs session Now,

//application/core/Front_controller.php

class Front_Controller extends MY_Controller
{
 function __construct()
     {
             parent::__construct();

   }
 }

another class under core/backend_controller

class backend_Controller extends MY_Controller
    {
     function __construct()
     {
             parent::__construct();
             $this->load->library('session');

     }
 }

now you can extend the front_controller for those controller you do not need session and extend backend controller which requires session from application/controller

Sagar Chapagain
  • 375
  • 4
  • 13