假设我在"application/controllers"中有两个控制器文件:base_controller.php和child_controllerphp
文件:base_controller.php:
class Base_controller extends CI_Controller{
parent::__construct();
// some functions
}
在child_controller.php中,我写道:
class Child_controller extends Base_controller {
parent::__construct();
// some functions
}
它抛出一个错误,表示找不到"Base_controller"。求你了,救命!
在CI中扩展核心类需要前缀(在配置文件中默认为"MY")。
您需要使用名为MY_Base_Controller的类来扩展CI_Controller,并将其保存在您的应用程序/库文件夹下。
之后,您可以使用您的子控制器对其进行进一步的子类。
有关的详细概述,请参阅此处
MY控制器
这是您的脚本
class Base_controller extends CI_Controller{
parent::__construct();
// some functions
}
在child_controller.php中,您可以编写:
class Child_controller extends Base_controller {
parent::__construct();
// some functions
}
您的儿童控制器应该有变化
include('base_controller.php'); //you should add this so that you can inherit//
class Child_controller extends Base_controller {
parent::__construct();
// some functions
}
更好的方法是创建MY_Controller,并将Base_Coontroller包含在MY_Contrller中,因为您知道MY_Controller将自动加载,它也将加载Base_Controlle器,将它们都保存在应用程序/核心目录中
Base_Controller
class Base_controller extends MY_Controller{
function __construct(){
parent::__construct();
}
// some functions
}
MY_Controller
class MY_controller extends CI_Controller{
function __construct(){
parent::__construct();
}
// some functions
}
include('Base_Controller'.EXT); // if this not work try following
include(APPPATH . 'core/Base_Controller'.EXT);
Child_Controller
class Child_controller extends Base_Controller{
function __construct(){
parent::__construct();
}
// some functions
}
文件:application/core/MY_Controller.php
class MY_Controller extends CI_Controller{
parent::__construct();
}
文件:application/core/Backend_Controller.php
class Backend_Controller extends MY_Controller {
parent::__construct();
}
文件:application/core/Admin_Controller.php
class Admin_Controller extends Backend_Controller {
parent::__construct();
}
设置自动加载:application/config/config.php
$config['proxy_ips'] = ''; //after this
/* load class in core folder */
function my_load($class) {
if (strpos($class, 'CI_') !== 0) {
if (is_readable(APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php' )) {
require_once (APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php');
}
}
}
spl_autoload_register('my_load');