Codeigniter extending controller, controller not found Codeigniter extending controller, controller not found codeigniter codeigniter

Codeigniter extending controller, controller not found


I've had the same issue in my first CI application and found two key elements that need to be checked:

1. Case Matching:Depending on your server configuration, the name of your file in the directory will need to match the case of your class. For instance, where your class is called "MY_Controller" your file name will need to be: "MY_Controller.php" on a Linux server. Windows servers have been known to have issues with uppercase filenames so you might experiment naming your controller "my_controller.php" and/or changing the extension to "my_" in your config.php instead of "MY_"

2. Insert an Autoloading functionFor reasons unknown to me, Codeigniter does not automatically recognize and read extended core classes before first loading the core class. This can cause issues with your extension not loading in correctly. To remedy this, you can add this simple autoloading script to the very bottom of your config.php

/*|--------------------------------------------------------------------------| Autoload Custom Controllers|--------------------------------------------------------------------------|*/function __autoload($class) {    if (substr($class,0,3) !== 'CI_') {        if (file_exists($file = APPPATH . 'core/' . $class . EXT)) {            include $file;        }    }}

Side note: the solution above was tested on CodeIgniter 2.1.4. The question asked involved CodeIgniter 2.1.2


Anyone reading this using CI 3+ and trying to attempt the same thing. Please note that the global EXT was depreciated when dropping php 4 support. You need to use the following now:

/*|--------------------------------------------------------------------------| Autoload Custom Controllers|--------------------------------------------------------------------------|*/function __autoload($class) {    if (substr($class,0,3) !== 'CI_') {        if (file_exists($file = APPPATH . 'core/' . $class . '.php')) {            include $file;        }    }}


I had the same problem, but if I created all controllers in the MY_Controller.php file all worked well.

<?phpclass MY_Controller extends CI_Controller{    function __construct()    {        parent::__construct();        // do some stuff    }}class MY_Auth_Controller extends MY_Controller{    function __construct()    {        parent::__construct();        // check if logged_in   }}