1

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?

Eduardo
  • 1,781
  • 3
  • 26
  • 61

2 Answers2

2

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

Swarna Sekhar Dhar
  • 550
  • 1
  • 8
  • 25
1

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

    ...
}
Nick
  • 2,593
  • 3
  • 30
  • 59
  • 1
    I will try that. Not including the class on the same file, but using a require inside it, I like to keep controllers files visible. – Eduardo Sep 19 '17 at 14:45
  • Good idea, one class per file is preferable – Nick Sep 19 '17 at 19:53
  • I posted another question, maybe you can help: https://stackoverflow.com/questions/46368601/loading-view-inside-a-library-issues-with-cached-vars – Eduardo Sep 22 '17 at 15:51