【发布时间】:2017-04-06 02:08:22
【问题描述】:
我正在尝试从application/controllers 中的子控制器调用application/core 中的核心控制器类。我在application/controllers 中有Home 控制器home.php,它在application/core 中扩展了Public_Controller。我的home.php是这样的:
<?php
class Home extends Public_Controller
{
public $data = array(
'page' => 'home',
'main_view' => 'home'
);
public function index()
{
$this->load->view($this->layout, $this->data);
}
}
在核心文件夹中,我有 public_controller.php
<?php
class Public_Controller extends MY_Controller
{
// Layout untuk "Publik"
public $layout = 'layout';
}
从上面的代码中,MY_Controller 也位于核心。这是代码
<?php
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
// Autoload model.
$model = strtolower(get_class($this));
if (file_exists(APPPATH . 'models/' . $model . '_model.php')) {
$this->load->model($model . '_model', $model);
}
}
}
所以,总而言之,我在application/controllers 中有控制器home,在application/core 中扩展public_controller,public_controller 在application/core 中扩展my_controller。
我已经通过添加此代码修改了 config/config.php
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
@include_once( APPPATH . 'core/'. $class . EXT );
}
}
所以我可以通过引用 https://philsturgeon.uk/codeigniter/2010/02/08/CodeIgniter-Base-Classes-Keeping-it-DRY/ 将基本控制器连接到控制器
但它仍然无法正常工作。这么说
致命错误:在中找不到类“Public_Controller” C:\xampp\htdocs\cipsb\application\controllers\home.php 在第 2 行
有什么办法可以解决这个问题吗? 谢谢
【问题讨论】:
标签: php codeigniter