Getting Started With CodeIgniter – 6

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
class MY_Controller extends Controller {

function __construct(){

function setup_assets(){
$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(){

function index(){
$this->load->view(‘publicuser/viewprivacy’, $this->data);



  1. http://Liz%20Jamieson says

    Hi – Brunno – Sorry about that. I need to overhaul the blog and fix a few problems. Here is the correct link :

  2. Hi,

    The part 2 is offline:

    You 404’d it. Gnarly, dude.

    Great site !

  3. http://Liz%20Jamieson says

    Hi Suman
    No the server doesn’t need to know anything. Once you have got your site working, you can make changes to the .htaccess file (assuming you are on Apache), that will allow you to make the urls produced by CI just a little friendlier – i.e – leave out the index.php which will appear in them if you don’t. Just get a couple of test pages working on your server at a test location, and see for yourself. The URLs are a little different but they are clean. It’s just the extra index.php that is also there that will prbably annoy you. But you can fix that as I said with .htaccess.


  4. http://Liz%20Jamieson says

    Hi Leo

    This is a bit late but you could review this forum post over at th CI forums :
    I think it contains the answer you are looking for.

  5. http://Suman says

    This might be off track question from the tutorial but still related to CI. I read that the CI use clean URL like as . Now I dont know how the server will know CI convention. Do something needs to be done to make server understand??

    Reply is greatly Anticipated!!!!


  6. Liz, first, thank you for your answer.

    Well, in the other hand i still having a problem, cause, like I say, i’m trying two have two MY_controllers. (MY_admin_controller, MY_main_controller). I have a -MY_controller- working perfect, with the others controllers, but, when I copy it, rename it MY_admin_controller, (and recode it MY_admin_controller extends…) and the caller categories.php (class Categories extend MY_admin_controller) it always says ..not found.
    Just for test it right when i rename it MY_controller.php again and change it to MY_controller extends Controller, it starts to work again O.o .
    I don’t know, it seems that MY_Controller is the only way i can name it. Could it be?
    Thanks again!!

  7. Nice tutorials with charming face 😀

  8. http://Liz%20Jamieson says

    If you get problems like “controller not found” it is probably to do with a naming issue.

    Check these three places :

    1) The Libraries Directory :
    Here the file is called say, MY_Controller.php

    2) Inside MY_Controller.php
    The class must be like this :
    class MY_Controller extends Controller
    function __construct(){
    your stuff here

    3) Within your controllers :
    class Contact extends MY_Controller {


  9. http://Leo says

    I made a MY_Controller.php file and put in the libraries, and then i call it from within several other controllers. But, then, i realize that i need 2 main (MY_controllers ). Basically I rename the file MY_admin_controller.php and init it: class MY_admin_Controller extends Controller {.. and when I call it from within x.php (controller)
    class x extends MY_admin_Controller it always says:
    Class ‘MY_admin_Controller’ not found.
    There’s something more than i need to change, or any rule for CI class and filenames, that makes this happens?
    Thank you a lot and excuse me for my english 😉

  10. http://Liz%20Jamieson says

    Glad to help.

  11. oh, that’s right! i thought “Controller” is just an example of controller name. thanks for this 🙂

  12. http://Liz%20Jamieson says

    It works with 1.7 for me.

    Did you name the mycontroller php file, like this : MY_Controller.php ?

  13. it doesn’t work at all with CI 1.7 :/ it is constantly telling me that the main class (MY_Controller) doesn’t exist.