How do I extend the code igniter controller class?


Question

In my CI system\libraries directory I have a new class named DD_Controller.php. This file looks like this:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class DD_Controller extends Controller 
{   
    protected $ddauthentication;


    function __construct()
    {           
        parent::Controller();
        $this->ddauthentication = "Authenticated";
    }
}
?>

My application controller is defined like this:

class Inquiry extends DD_Controller 
{...}

The Inquiry class works fine when I extend Controller, but I get a

Fatal error: Class 'DD_Controller' not found in C:\development\localhost\applications\inquiry\controllers\inquiry.php on line 4

When I extend DD_Controller. In the config file I have the prefix defined as such:

$config['subclass_prefix'] = 'DD_';

Any idea of what I'm missing?

TIA

1
10
10/26/2009 5:57:26 PM

Accepted Answer

DD_Controller.php should be in /system/application/libraries/

If you're using the same CI for multiple apps, and you want them all to be able to extends their controllers to your custom one then you can extend the base Controller class in the same file.

In system/libraries/Controller.php below the Controller class:

class Mega_Controller extends Controller {
    function Mega_Controller()
    {
        parent::Controller();
        // anything you want to do in every controller, ye shall perform here.
    }
}

Then you'll be able to do this in your app controllers:

class Home extends Mega_Controller {
    ....

Since the extended controller class you created will be available. I think this is better then overwriting the base controller, but that would work as well.

4
10/26/2009 7:59:05 PM

This is a better approach. Do the following:

  1. Go to the following directory: your_ci_app/application/core/ and create a php file called MY_Controller.php (this file will be where your top parent classes will reside)
  2. Open this the file you just created and add your multiple classes, like so:

    class Admin_Parent extends CI_Controller {
        public function __construct() {
            parent::__construct();
        }
    
        public function test() {
            var_dump("from Admin_Parent");
        }
    }
    
    class User_Parent extends CI_Controller {
    
        public function __construct() {
            parent::__construct();
        }
    
        public function test(){
            var_dump("from User_Parent");
        }
    
    }
    
  3. Create your children controllers under this directory your_ci_app/application/controllers/ . I will call it adminchild.php

  4. Open adminchild.php and create your controller code, make sure to extend the name of the parent class, like so:

    class Adminchild extends Admin_Parent {
    
        function __construct() {
            parent::__construct();
        }
    
        function test() {
            parent::test();
        }
    
    }
    

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon