[eluser]toopay[/eluser]
Having bunch of lines in EVERY controller constructor, not just about having ugly and un-maintainable code, but it's indeed break the OO rules itself.
Since this is an OO issues, there are one thing you shouldnt missed : Object Inheritance. In this case, you may consider to this :
1. Declare a "base" controller for each type of your controller.
2. Create some automatic procedure that separate your controller type.
3. Specify some global variable or function, so you (D)ont ®epeat (Y)ourself.
You can do that by creating MY_Controller.php, and put that into your 'core' folder inside application folder. I just will show you the idea...
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/* This is the main Base Controller, a place for declare a global variable or some handy function */
class Base_Controller extends CI_Controller {
protected $is_login;
protected $other_important_globalvar;
public function __construct()
{
parent::__construct();
// Declare your common class/library here, or in config/autoload.php
$this->load->library('proxy');
$this->load->library('session');
$this->load->helper('url');
// ... and so on
// Here you can define the user state as a global variable
// which accessible from all child controller which extends this class
$this->is_login = (bool) $this->yourauthlib->is_login();
// ... and so on
}
}
/* This is the main Public Controller, a parent controller for all public/accessible controller */
class Public_Controller extends Base_Controller {
protected $var_public;
public function __construct()
{
parent::__construct();
// Here you can load additional class/library which common used by your public controller
$this->load->library('dbhelper');
// You also can declare some common variable
$this->var_public = 'foo';
}
}
/* This is the main Admin Controller, a parent controller for all controller which only accessible after user authenticated by system */
class Admin_Controller extends Base_Controller {
protected $var_admin;
public function __construct()
{
parent::__construct();
// If user not authenticated, send them to login page
$this->is_login == TRUE or redirect('login');
// Here you can load additional class/library which common used by your admin controller
$this->load->library('xmlrpc');
$this->load->library('xmlrpcs');
// You also can declare some common variable
$this->var_admin = 'bar';
}
}
Now, you can use above type of controller: you can extends from Public_Controller for all your accessible controller, and extends from Admin_Controller to the rest of your controller which need to authenticate user.
And since you already declare a global variable, like user authentification checks (it only run once) you doesn't need to repeat an authentification procedure and execute a library function everywhere in your controller to check user state, now you can use $this->is_login to determine that.