Eduardo
Eduardo

Reputation: 1831

Extending CodeIgniter core classes

I have in my core folder, 2 controllers:

  1. MY_Controller
  2. MY_AdminController

Both extend CI_Controller. First one works great, but second one no, when I call the controller which is inheriting from MY_AdminController I get an error:

Fatal error: Class 'MY_AdminController' not found

After doing some research I found:

https://codeigniter.com/user_guide/general/core_classes.html

In this document it says you use "MY_" prefix (possible to change it in config) to extend core classes, I am doing that.

What am I missing?

UPDATE I am wondering if the problem is because since I am creating a file inside "core" folder, CI checks if it does exist an original on its own core with same name but prefix CI?

Upvotes: 1

Views: 2525

Answers (2)

Swarna Sekhar Dhar
Swarna Sekhar Dhar

Reputation: 548

to allow any new class . means which class are note defined in system/core solution could be as follow.

Try putting below function at the bottom of config file

/application/config/config.php

function __autoload($class) {
    if(strpos($class, 'CI_') !== 0)
    {
        @include_once( APPPATH . 'core/'. $class . '.php' );
    } }

My core class My_head not found in codeigniter

Upvotes: 2

Nick
Nick

Reputation: 2641

Here is a good explanation as to why you can't do it the way you have described. Essentially CodeIgniter looks for ['subclass_prefix'] . $Classname, e.g. 'MY_' . 'Controller'. And here is the same question for CI2

Solution:
Put both MY_Controller and MY_AdminController in MY_Controller.php

MY_Controller.php

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

    ...
}

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

    ...
}

Upvotes: 1

Related Questions