Search code examples
phpcodeignitercodeigniter-2

Upgrading CodeIgniter 1.7.2 to 2.1.4 causes Fatal error: Class 'MY_Controller' not found


I finally decide Upgrade my CI, I follow this user guide CodeIgniter User Guide Version 2.1.4

but in the end when I try to load my web I get error like this

    Fatal error: Class 'MY_Controller' not found in 
C:\xampp\htdocs\myfolder\system\application\controllers\home.php on line 6

and in home.php line 6 is class Home extends MY_Controller {

I have file with name My_controller.php in

C:\xampp\htdocs\myfolder\system\application\libraries

this is the file:

<?php

class  MY_Controller  extends  Controller {

  var $needLogin = FALSE;
  var $rakunpanel = FALSE;

  function __construct() {  
    parent::__construct();

    $isVisited = $this->session->userdata('visited');
    if($isVisited !== '1'){
      $this->session->set_userdata('visited', '1');
      redirect('welcome');
    }

    if($this->rakunpanel === TRUE){
      $this->needLogin = TRUE;
      $this->layout->setLayout('folderAdmin/layout_admin');
    }

    if($this->needLogin === TRUE && !$this->tank_auth->is_logged_in()){
      redirect(ADMIN_PATH.'auth/login/');
    }

  }

  function is_ajax() {
    return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
  }  
}  

it's hard for me cause I must follow step upgrade from CI version 1.7.2 to 2.1.4 so any suggest, why that's happen? thank you for all advice.


Solution

  • First: rename your My_controller.php to MY_Controller.php.

    Second: change the parent class name to CI_Controller:

    class MY_Controller extends CI_Controller { ... }
    

    And finally put MY_Controller.php file inside application/core folder.

    From the User Guide:

    For example, to extend the native Input class you'll create a file named application/core/MY_Input.php, and declare your class with:

    class MY_Input extends CI_Input {
    
    }