问题
class Settings extends USER_Controller {...}
class USER_Controller extends MY_Controller {...}//limit access to user and define some params and functions that is user depended
class MY_Controller extends CI_Controller {...}//provide extra functionality accross all controllers
well if i try to create above 3 class's this will not work.
yet if i just use settings extends MY_Controller
it will work fine.
so is there a way i can put a middle class between my controller and MY_Controller -the base controller that extends CI_Controller ?
回答1:
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
inapplication/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.
回答2:
Place the user_controller class file at the end of MY_Controller.php
MY_Controller.php in /application/core/
class MY_Controller extends CI_Controller {...}
class USER_Controller extends MY_Controller {...}
Now from your controller in your controllers folder, you can extend the controller from USER_Controller:
class Settings extends USER_Controller {...}
回答3:
What did the best job for CI 2.1.4:
application/core
create for example:
- base_controller (base instructions, extending from ci_controller)
- admin_controller (extending from basecontroller and adds authentication instructions)
Then use them in: application/controllers:
for example:
class users extends admin_controller...
回答4:
You could use Phil's core solution, its simple and easy.
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
回答5:
You can extend the class the way you want but the problem is loading the class, you have to include your USER_Controller
your Settings
Class. Problem is not extend problem is including the file, so you have to find a way to include the second class.
See http://www.php.net/manual/en/function.spl-autoload.php
来源:https://stackoverflow.com/questions/21663045/creating-a-core-class-that-extends-another-core-class-codeigniter