1
votes

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?

2
Make sure your filename matches the class application core/MY_Admincontroller.php - Mr. ED
@wolfgang1983 Thanks for the answer. I have edited my question, it was a typo, on my code I have the same as class name an file name - Eduardo
There is no CI_Admincontroller, so you can't extend something that's not there. - Brian Gottier
I am extending CI_Controller @BrianGottier - Eduardo

2 Answers

2
votes

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

1
votes

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();
    }

    ...
}