Extending The Controller Class for Common Code
I’d like to thank Noah Botimer for his interest and assistance. I’ve now changed the way I’ve constructed my fledgling web application. This code replaces the code shown in Getting Started With CodeIgniter 5.
Because I wanted to have a certain amount of common code in all controllers – calls to two functions called setup_assets and setup_system_data in my case, I have now extended the controller class by using the MY_ technique.
The MVC example is below.
The Extended Controller Class
This is placed in system/application/libraries/MY_Controller.php
<?php
class MY_Controller extends Controller {
function __construct(){
parent::__construct();
$this->setup_assets();
$this->setup_system_data();
}
function setup_assets(){
$this->load->database();
$this->load->model('systemdata');
$this->data['base_url'] = $this->systemdata->get_base_url();
$this->data['cssmain'] = $this->systemdata->get_cssmain_filespec();
$this->data['pdf'] = $this->systemdata->get_pdf_filespec();
$this->data['cssIE6'] = $this->systemdata->get_cssie6_filespec();
$this->data['cssIE7'] = $this->systemdata->get_cssie7_filespec();
$this->data['scripts'] = $this->systemdata->get_scripts_filespec();
$this->data['css'] = $this->systemdata->get_css_filespec();
$this->data['xml'] = $this->systemdata->get_xml_filespec();
$this->data['flash'] = $this->systemdata->get_flash_filespec();
$this->data['site_images'] = $this->systemdata->get_site_images_filespec();
$this->data['images'] = $this->systemdata->get_images_filespec();
$this->data['public_status'] = $this->systemdata->get_public_status();
$this->data['member_status'] = $this->systemdata->get_member_status();
}
function setup_system_data(){
$this->data['page_name'] = $this->systemdata->get_page_name();
$this->data['copy_right_range'] = $this->systemdata->getcopyrightrange();
$this->data['current_year'] = $this->systemdata->selectcurrentyear();
$this->data['version'] = $this->systemdata->selectwebsiteversion();
$this->data['global_phone'] = $this->systemdata->selectglobalphone();
$this->data['uk_phone'] = $this->systemdata->selectukphone();
$this->data['any_phone'] = $this->systemdata->selectanyphone();
}
}
?>
A Controller
<?
class Privacy extends MY_Controller {
var $data;
function Privacy(){
parent::__construct();
$this->load->helper('url');
}
function index(){
$this->load->view('publicuser/viewprivacy', $this->data);
}
}
?>




