Codeigniter extending controller, controller not found

26,500

Solution 1

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

<?php

class 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
   }
}

Solution 2

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 function For 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

Solution 3

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

Solution 4

Yes, with core's MY_ classes you can override ONLY codeigniter framework entities such as Controller, Model, Config, Exception and so on. Please refer to https://ellislab.com/codeigniter/user-guide/general/core_classes.html

Share:
26,500
Harlsten
Author by

Harlsten

BY DAY: working as a C# (ASP MVC, SQL Server, DevExpress) software developer for small company BY NIGHT: Trying to build own inventory system in php, mysql, angular with android app, want to build own startup

Updated on July 05, 2022

Comments

  • Harlsten
    Harlsten almost 2 years

    In Codeigniter 2.1.2 I want to create base controller and then extends from this controller. It does not work and I have no idea why and I'm pretty desperate now.

    In \application\core\MY_Base_Controller.php I have this:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class MY_Base_Controller extends CI_Controller 
    {
        function __construct()
        {
            parent::__construct();
    ...
    

    In \application\controllers\Home.php I have this:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
    
    class Home extends MY_Base_Controller {
    

    And the error message is

    Fatal error: Class 'MY_Base_Controller' not found in ...\application\controllers\Home.php on line 3

    I have no idea what to do, I read all over the internet that I have to put base controller to core folder what I did, that I have to name base controller with prefix MY_, I did. But it is still no working. And in my config.php is this line as well

    $config['subclass_prefix'] = 'MY_';
    

    Im running this on localhost using xampp

    Thank you for your help

    EDIT

    Could someone please downlod following link try it, and tell me whats wrong. I have just downloaded codeigniter tried to create base controller and extend welcome controller. Not working. In following rar there are just modified files. Thank you http://goo.gl/sKHkDl

    EDIT 2

    I'm able to get this working by renaming MY_Base_Controller to MY_Controller. Does this mean, I'm able to create only one extended class for a controller ? eg. I can not have

    • MY_Base_Auth_Controller
    • MY_Base_Article_Controller

    Just and only MY_Controller ?

  • Mickäel A.
    Mickäel A. almost 10 years
    I wanted to add mutiple custom controllers in application/core like so (<- means extends) : core/MY_Top <- core/MY_Second <- controllers/Connection & other controllers... but CI wasn't recognizing MY_Second until I added the given __autoload() method you've given. Thanks a lot
  • joni_demon
    joni_demon over 9 years
    thanks for this solution..it's awesome.. save my day
  • loki9
    loki9 about 9 years
    is this feature available on CI3?
  • Randika Vishman
    Randika Vishman about 9 years
    Simple yet powerful solution! I've followed your way and it works fine for me too!
  • Silvio Delgado
    Silvio Delgado over 8 years
    But you can use the __autoload() function to skip this limitation.
  • MiddleCloud
    MiddleCloud over 8 years
    @loki9 i think so because __autoload() is a PHP core function. See php.net/autoload
  • Mani
    Mani about 8 years
    If you have more than one controllers and many functions in each controller, you are gonna suffer.
  • HelloSpeakman
    HelloSpeakman over 7 years
    This is a terrible practice, you'd be much better off fixing the naming convention issue than doing this. (See Websmith's answer)
  • MrCarrot
    MrCarrot over 6 years
    Kind of unrelated, but in Windows My_Controller.php works, but on Linux it is case sensitive and needs to be MY_Controller.php - thought I would mention it as this caught me out
  • Alex
    Alex over 6 years
    @Alexander Yagovdik answer explains the "For reasons unknown to me" part. Was also racking my brain as to why.
  • Sumit patel
    Sumit patel about 6 years
    nice Answer...Excellent.