0

I have various controller is core/ folder named core/my_controller.php and other controllers in libraries/ folder as libraries/user_controller.php,libraries/frontend_controller.php. Now I am using this code below in config.php to autoload these files. But I dont think its working.

I am seeing this error message Fatal error: Class 'MY_Controller' not found in /home/manumakeadmin/manumake.com/2d/application/libraries/frontend_controller.php on line 3

function __autoload($classname) {

    if (strpos($classname, 'CI_') !== 0) {
        $file = APPPATH . 'libraries/' . $classname . '.php';
        if (file_exists($file) && is_file($file)) {
            @include_once($file);
        }
    }
}

EDIT

I can make it work by manually including the files as

<?php
include_once APPPATH.'core/my_controller.php';

class Frontend_controller extends MY_Controller
{

But I was wondering if I could make the autoload code to work

prakashchhetri
  • 1,806
  • 4
  • 36
  • 61

2 Answers2

2

There is also autoloading non-ci classes technique documented in those two links and mentioned before.

Adding an snippet to config.php to load those classes does the trick.

function __autoload($class)
{
    if (substr($class,0,3) !== 'CI_')
    {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT))
        {
            include $file;
        }
    }
}

and adding your base class in application/core/Base_Controller.php

Community
  • 1
  • 1
yuvilio
  • 3,795
  • 32
  • 35
1

Library file names and class names must match and be capitalised - Frontend_controller.php

When extending a core class, the file name must also match the class name. Capitalise the prefix and the first letter of the class name in the file: MY_Controller.php The prefix can be set in: application/config/config.php

Also ensure that your files are in the application directory, rather than system. This seems to be the case, but it's worth checking.

It's always a good idea to check the user guide for naming conventions. For example, model class names must have the first letter capitalised and the rest lowercase; the file name should be all lower case and match the class name.


However, it's very important to realise that libraries in CodeIgniter aren't intended for extending core classes, for example CI_Controller, which I assume MY_Controller is extending. Libraries should be used to:

  • create entirely new libraries.
  • extend native libraries.
  • replace native libraries.

I think it's likely that your Frontend_controller would be better located in: application/controllers/

jleft
  • 3,457
  • 1
  • 23
  • 35
  • Thanks a lot. But Once I was doing the same thing in my linux server, and I got error messages because I was using the filenames in capital letter to match them with model name – prakashchhetri May 03 '13 at 05:41
  • No worries! You're correct - [model file names should be all lower case](http://ellislab.com/codeigniter/user-guide/general/models.html#anatomy). It's always a good idea to check the naming conventions in the user guide. – jleft May 03 '13 at 07:51