Assuming you are using Codeigniter 2, you can do this by placing all the extended controller classes in the same file.
In / application / core, create a file called MY_Controller.php (remember to check the subclass prefix in config.php around line 109)
Here you can add all controller classes for expansion. For example:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { public function __construct() { parent::__construct(); $this->form_validation->set_error_delimiters('<div class="form-error">', '</div>'); } } class LoggedIn extends MY_Controller { public function __construct() { parent::__construct(); if (is_logged_in() == FALSE) { $this->session->set_userdata('return_to', uri_string()); $this->session->set_flashdata('message', 'You need to log in.'); redirect('/home'); } } } class AdminLayout extends LoggedIn { public function __construct() { parent::__construct();
Then, when you create your controllers in normal mode, simply select the base controller class to extend. Example
class Foo extends AdminLayout { public function __construct() { parent::__construct(); if (is_logged_in() == FALSE) { $this->session->set_userdata('return_to', uri_string()); $this->session->set_flashdata('message', 'You need to log in.'); redirect('/home'); } } }
or
class Bar extends ModLayout { public function __construct() { parent::__construct(); if (is_logged_in() == FALSE) { $this->session->set_userdata('return_to', uri_string()); $this->session->set_flashdata('message', 'You need to log in.'); redirect('/home'); } } }
Rooneyl
source share