0

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.

5
  • have you tried it moving in to libraries folder? Feb 13, 2013 at 14:26
  • yep this was the first thing I did.
    – bobo2000
    Feb 13, 2013 at 14:26
  • try removing the lines you added in config file and check Feb 13, 2013 at 14:27
  • Still doesn't work. :(
    – bobo2000
    Feb 13, 2013 at 14:32
  • 5
    some bad practice in the code. use __construct() instead of function my_Controller(). use public instead of var. for readibility, explicitely call the functions public/private/protected these wont solve your issues but it is time we should adapt to new desgins and syntaxes.
    – itachi
    Feb 13, 2013 at 14:34

1 Answer 1

1

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.

2
  • Right MY_Controller.php (with caps is in application core) and CI_Controller.php is in system/core/ it is still throwing the same error. My php version is 5.3.19 could this be affecting it?
    – bobo2000
    Feb 13, 2013 at 15:27
  • Seem to have fixed it, the problem was the syntax. It did not like function my_Controller(){
    – bobo2000
    Feb 13, 2013 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.