duyvu1311
duyvu1311

Reputation: 97

Inheritance between controllers in Codeigniter

Suppose that I have 2 controller files in 'applcation/controllers': base_controller.php and child_controller.php

File: base_controller.php:

class Base_controller extends CI_Controller{
    parent::__construct();

// some functions
}

In child_controller.php, I write:

class Child_controller extends Base_controller {
    parent::__construct();

// some functions
}

It throw an error mean that 'Base_controller' not found. Please, help!

Upvotes: 0

Views: 4735

Answers (4)

antelove
antelove

Reputation: 3348

File: application/core/MY_Controller.php

class MY_Controller extends CI_Controller{
    parent::__construct();

}

File: application/core/Backend_Controller.php

class Backend_Controller extends MY_Controller {
    parent::__construct();

}

File: application/core/Admin_Controller.php

class Admin_Controller extends Backend_Controller {
    parent::__construct();

}

Set autoload: application/config/config.php

$config['proxy_ips'] = ''; //after this 

/* load class in core folder */
function my_load($class) {

    if (strpos($class, 'CI_') !== 0) {    
        if (is_readable(APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php' )) {    
            require_once (APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php');    
        }
    }

}

spl_autoload_register('my_load');

Upvotes: 0

umefarooq
umefarooq

Reputation: 4574

Hi better way is to create MY_Controller and include Base_Controller in MY_Controller as you know MY_controller will be autoload it will also load Base_Controller too, keep them both in application/core directory

Base_Controller

class Base_controller extends MY_Controller{
    function __construct(){
          parent::__construct();
    }

// some functions
}

MY_Controller

class MY_controller extends CI_Controller{
    function __construct(){
          parent::__construct();
    }

// some functions
}
include('Base_Controller'.EXT); // if this not work try following
include(APPPATH . 'core/Base_Controller'.EXT);

Child_Controller

class Child_controller extends Base_Controller{
        function __construct(){
              parent::__construct();
        }

    // some functions
    }

Upvotes: 0

Saswat
Saswat

Reputation: 12836

Here is your script

class Base_controller extends CI_Controller{
    parent::__construct();

// some functions
}

In child_controller.php, you write:

class Child_controller extends Base_controller {
    parent::__construct();

// some functions
}

There should be a change in your child controller

include('base_controller.php'); //you should add this so that you can inherit//
class Child_controller extends Base_controller {
    parent::__construct();

// some functions
}

Upvotes: 1

Michael O'Brien
Michael O'Brien

Reputation: 401

Extending core classes in CI requires a prefix (which is 'MY' by default in the config file).

You'll need to extend CI_Controller with a class called MY_Base_controller and save it under your application/libraries folder.

After that point you can sub-class it further with your child controller.

See here for a detailed overview

MY Controller

Upvotes: 2

Related Questions