【问题标题】:Codeigniter cannot call core classCodeigniter 无法调用核心类
【发布时间】: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_controllerpublic_controllerapplication/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


    【解决方案1】:

    看起来我没有注意我正在使用的 Codeigniter 版本。我使用 codeigniter 3.1.0,我使用的参考是旧的 codeigniter 版本。对于 codeigniter 3,只需在 config/config.php 中使用它

    function __autoload($class) {
        if (substr($class,0,3) !== 'CI_') {
            if (file_exists($file = APPPATH . 'core/' . $class . '.php')) {
                include $file;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2013-08-14
      • 2014-03-22
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多