Reputation: 1816
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
Upvotes: 0
Views: 6811
Reputation: 4175
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
Upvotes: 2
Reputation: 3457
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/
Upvotes: 1