8

i’m using the Codeigniter.2.1.3 for a website, so i need to extend the CI_Controller so i can add a method to be executed with all controllers so i did what’s in the user_guide:

creating a file named MY_Controller.php in the application/core folder the creating in it MY_Controller Class that extends the CI_Controller, the changing my regular controller to extend the MY_controller like this: MY_controller.php:

class MY_Controller extends CI_Controller{
    protected $page;
    # Constructor
    function __construct (){
        parent::__construct();
        #code shared with all controllers
    }
    public function get_page(){
        #code to get_the right page here
    }
}

regular controller named Regular.php:

class Regular extends MY_Controller{
     public function __construct(){
         parent::__construct();
     }
     public function index(){
          $this->get_page();
     }
}

but the following error keep appearing:

Fatal error: Class ‘MY_Controller’ not found in /var/www/immo/CodeIgniter_2.1.3/application/controllers/regular.php on line 2

j0k
  • 22,600
  • 28
  • 79
  • 90
Yahya KACEM
  • 2,481
  • 7
  • 34
  • 61
  • 2
    In your application/config/config.php what do you have for `$config['subclass_prefix']` – Esailija Nov 20 '12 at 23:05
  • 7
    Are you sure MY_Controller.php is in the application/core folder? Also if your on linux, things can be case sensitive :) Make sure the file name is the same as the controller name. `MY_Controller.php`, not `MY_controller.php`. – Jeemusu Nov 21 '12 at 01:15
  • add a space after MY_Controller – Ryan Nov 21 '12 at 02:00
  • @Esailija $config['subclass_prefix'] = "MY_" – Yahya KACEM Nov 21 '12 at 04:36
  • @Jeemusu: yes its in the application/core folder – Yahya KACEM Nov 21 '12 at 04:37
  • @YahyaKACEM please turn log level to the max (set application/config/config.php `$config['log_threshold']` to 4) then make /application/logs writable and see what happens in codeigniter – Esailija Nov 21 '12 at 10:24
  • Try renaming your PHP file to `MY_controller.php` (lower case C) and placing it in your libraries folder `application/core` (sorry wrong path) – Gavin Nov 21 '12 at 14:50

5 Answers5

12

You would need to include your MY_Controller class or auto-load it. I suggest you auto-load it by adding the following to your application/config/config.php file.

function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT))
        {
            include $file;
        }
    }
} 
Maxime Morin
  • 2,008
  • 1
  • 21
  • 26
  • 2
    why do i need to do that when i extend a library i don't need to autoload it why this is different, and there's nothing about that in the documentations. – Yahya KACEM Nov 21 '12 at 04:40
  • Here is the original article on the technique which was created by Phil Sturgeon - http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-base-Classes-Keeping-it-DRY – Jeemusu Nov 21 '12 at 06:07
  • still not working now it gives me 2 new warnings: include_once(application/core/MY_Controller.php): failed to open stream: No such file or directory – Yahya KACEM Nov 21 '12 at 06:51
  • include_once(): Failed opening 'application/core/MY_Controller.php' for inclusion (include_path='.:/var/www/ZendFramework/library') – Yahya KACEM Nov 21 '12 at 06:52
  • no idea whet that mean in the application/core there's the file MY_Controller.php and its readable. – Yahya KACEM Nov 21 '12 at 06:53
  • Try to output the content of APPPATH and see if it does point to the right path. – Maxime Morin Nov 21 '12 at 13:32
  • And your `index.php` file is at the same level than your `application` folder? – Maxime Morin Nov 21 '12 at 13:57
  • this is so embarrassing it was just the wrong file name instead of MY_Controller.php i save the file with the name MY_Contorller.php and no need for the autoload function in the config file it’s already autoloaded. sorry for that and thanx for your help. – Yahya KACEM Nov 21 '12 at 15:13
  • this autoload function is useful when you extends class more then 2 inheritance, I mean, just like in philsturgeon blog. – Ifan Iqbal Sep 21 '13 at 03:06
  • Deprecated: __autoload() is deprecated, use spl_autoload_register() – Deepak Keynes May 31 '20 at 02:55
4

Make sure the filename is perfectly cased. Linux server is case-sensitive. So if the class name is My_Controller then the name of the file should be My_Controller.php

Dibyendu Mitra Roy
  • 1,604
  • 22
  • 20
  • 1
    It doesn't matter what the controller class name is inside. Just having `MY_Controller.php` will solve the problem. In my case, my controller's name is CRM_controller.. :) – Gogol Oct 17 '14 at 09:38
3

in config/config.php

/* load class in core folder */
function my_load($class) {        

    if (strpos($class, 'CI_') !== 0) {            
        if (is_readable(APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php' )) {                
            require_once (APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php');                
        }
    }

}

spl_autoload_register('my_load');
antelove
  • 3,216
  • 26
  • 20
  • Despite all the other solutions did kind of make sense, this is the only one that actually helped. I believe the Classes in the core folder don't outoload in my case because I upgraded to CodeIgnitor 3.x from 2.x (but that needs to be confirmed) and I may have forgotten something during the process... – Edwin Krause Mar 21 '20 at 10:22
2

$config['subclass_prefix'] = "MY_"

check that in config.php and of course you should use it in Controller Name Like MY_Controller.php and Named "class MY_Controller...."

TarangP
  • 2,711
  • 5
  • 20
  • 41
ronihasan
  • 21
  • 7
0

Late with this answer, but I got the

"Fatal error: Class ‘MY_Controller’ not found" error

when I had a controller (.php) file by the same name at the web root instead of the application/controllers directory.

Don't know how it got there, actually, but the problem went away when I deleted it.

Community
  • 1
  • 1
ScottyB
  • 2,167
  • 1
  • 30
  • 46