Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter MY_Controller not found

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

like image 786
Yahya KACEM Avatar asked Nov 20 '12 22:11

Yahya KACEM


3 Answers

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;
        }
    }
} 
like image 190
Maxime Morin Avatar answered Nov 11 '22 23:11

Maxime Morin


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

like image 44
Dibyendu Mitra Roy Avatar answered Nov 12 '22 00:11

Dibyendu Mitra Roy


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');
like image 3
antelove Avatar answered Nov 12 '22 01:11

antelove