【发布时间】: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.phpapplication\libraries\REST_Controller.phpapplication\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