In application development logged in check is needed almost everywhere. To achieve this generally we make a function and call it at the bottom of page. In codeigniter we called it in __construct() or make a hook.
But now codeigniter has a nice feature. Now codeigniter allow developer to extend core. So you can extend every core classes without changing core files. Even codeigniter allow override of core methods.
So extending CI_Controller we can easily handle logged in check like this
First create MY_Controller.php in application/core folder.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $view_data = array();
function __construct()
{
parent::__construct();
}
}
class MY_Restricted_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
// Add logged in checking here
}
}
And now extend your controllers like this:
Which controller is restricted
class Controller_name extends MY_Restricted_Controller {
.
.
.
}
Which controller is public
class Controller_name extends MY_Controller {
.
.
.
}
Here we also can add some common actions like loading, profiling etc.
This way it is really nice to manage application nicely.