I advise you to read the following two articles:
Phil will introduce you to how to create parent controllers whose constructors will contain the session and potentially database logic. All controllers that you create thereafter should inherit from your custom controllers instead of the native CI_Controller
.
Followed by....
Shane's article revamps Phil's technique and relocates your custom controllers from /core
to /base
and also utilizes a better __autoload()
'er. This implementation allowed me, for instance, to use CodeIgniter's CLI class, whereas, Phil's bugged out.
To give you an idea - your code would look a little something like this once complete:
In /base/MY_In_Controller.php
:
<?php
class MY_In_Controller extends CI_Controller{
function __construct(){
parent::__construct();
//things like:
//is the user even logged in? thank heavens I don't have to check this in every controller now. redirect if the session doesnt exist.
//query the database and grab the permissions for the user. persist them with $this->load->vars();
$this->data['perms'] = some_database_function();
$this->load->vars($this->data);
}
}
In controllers/manage.php
:
<?php
class Manage extends MY_In_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->view('manage');
//and I can still access their permissions here and in the view.
print_r($this->data['perms']);
}
}