As @Minhaz Ahmed said, the problem is loading the class. Codeigniter only loads the class with the subclass prefix as in,
https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/CodeIgniter.php#L276
if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
{
require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
}
So after some diggings I have came with a solution using both codeigniter hooks and spl_autoload_register()
for my project without hacking the CORE. Please follow the steps to achieve what's in THE QUESTION.
Enable hooks in the config file if it isn't already.
create a hook in application/config/hooks.php as below.
$hook['pre_system'] = array(
'class' => '',
'function' => 'autoload',
'filename' => 'autoload.php',
'filepath' => 'hooks',
'params' => ''
);
Create the autoload.php
in application/hooks
folder.
inside the autoload.php
file,
function autoload()
{
spl_autoload_register(function($class)
{
if(strpos($class,'CI_') !== 0 && file_exists(APPPATH.'core/'.$class.EXT))
{
require_once APPPATH . 'core/' . $class . EXT;
}
});
}
That's it.
Note: Here, I've used pre_system
hook, not pre_controller
, since codeigniter loads the base controller classes between pre_system
and pre_controller
hooks.
Hope this helps. If there are any issues with this please do comments.