bobo2000
bobo2000

Reputation: 1877

My Controller not extending

I have recently upgraded my codeigniter version from 1.7 to 2.1. However, I am struggling to extend my my_controller class, previously it was working fine when put in the app->libraries folder.

With CI instructions I have moved it too app -> core directory, the code is as follows:

    class MY_Controller extends CI_Controller 
{
    var $data=array();
    var $secured=false;
    var $area="user";
    function my_Controller(){

         parent::__construct();




        // loading libraries etc
        $this->load->library('session');
        $this->load->helper('url');                 
        $this->load->helper('common');

[..]

And in the config file I have added the following line of code as demonstrated from this tutorial:

http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-base-Classes-Keeping-it-DRY

    /*
| -------------------------------------------------------------------
|  Native Auto-load
| -------------------------------------------------------------------
| 
| Nothing to do with cnfig/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
*/
function __autoload($class)
{
 if(strpos($class, 'CI_') !== 0)
 {
  @include_once( APPPATH . 'core/'. $class . EXT );
 }
}

Yet I am still getting the following PHP error:

PHP Fatal error: Class 'MY_Controller' not found in C:\inetpub\wwwroot\latestCI\application\controllers\home.php on line 5

Any idea why this is not working, thanks

Edit:

Seemed to have gotten it to work. Basically MY_Controller has to follow this structure:

 <?php
class MY_Controller extends CI_Controller {

function __construct()
{
    parent::__construct();
//constructor code here
}

//Custom functions here
}


?>

Otherwise it will throw an error.

Upvotes: 0

Views: 186

Answers (1)

complex857
complex857

Reputation: 20753

You will have to place your MY_Controller.php file (captialization is important!) under application/core/. This folder is introduced (among others) with codeiginiter version 2.

When in doubt, check where the original class's file located under the system/ directory and mirror that under application/.
So if the CI_Controller.php is under system/core/ then the MY_Controller (which extends CI_Controller) should go to application/core.

Upvotes: 1

Related Questions