【问题标题】:Codeigniter give class not found error when i use MY_Controller当我使用 MY_Controller 时,Codeigniter 给出类未找到错误
【发布时间】:2017-12-04 09:11:06
【问题描述】:

问题
当我尝试致电http://my_url/api/login/gethomescreen 时,它给了我错误Fatal error: Class 'MY_API_Controller' not found
当我添加Login.php require APPPATH . '/core/MY_API_Controller.php'; 的以下行顶部时,它工作正常。
当我在 config.php 中添加 __autoload() 并加载 CI_Controller 时,它也可以工作

但根据 codeigniter 结构,它应该在没有包含文件的情况下工作。当我扩展父类时,它应该会自动加载。我只想知道为什么会这样?
这是我的代码和路径详细信息。
Login.php

class Login extends MY_API_Controller {
    public function __construct() {
        parent::__construct();
    }
    public function gethomescreen_post(){
        //my code goes here
    }
}  

MY_API_Controller.php

ob_clean();
require APPPATH . '/libraries/REST_Controller.php';
class MY_API_Controller extends REST_Controller {

    protected $_options = array();

    public function __construct() {
        parent::__construct();

        // Load the jwt.php configuration file
        if (empty($options)) {
            $this->load->config('jwt', true);
            $options = $this->config->item('jwt');
        }
        $this->_options = $options;
        $this->load->model('users_model');
    }
}  

REST_Controller.php

abstract class REST_Controller extends CI_Controller {
public function __construct($config = 'rest') {
        parent::__construct();

        $this->preflight_checks();
        //further code....
        }
}  

文件路径

application\controllers\api\Login.php
application\libraries\REST_Controller.php
application\core\MY_API_Controller.php

【问题讨论】:

  • Rest Controller 不是核心类 - 更多位于 codeigniter.com/userguide3/general/core_classes.html
  • 是的,知道了。但是当我调用 Login api 时,为什么它没有加载?或者加载它需要 CI_Controller ?
  • 将 MY_API_Controller.php 放入 application/controllers/application/libraries/。 CI 不会在core 或任何子文件夹中查找它(不使用和自动加载器)。

标签: php rest codeigniter class codeigniter-3


【解决方案1】:

在你的 config 文件夹中的 config.php 文件中,是否设置了这一行:

$config['subclass_prefix'] = 'MY_';

到:

$config['subclass_prefix'] = 'MY_API_';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 2016-05-04
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2015-09-19
    相关资源
    最近更新 更多