加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 找不到codeigniter MY_Controller

发布时间:2020-12-13 18:27:29 所属栏目:PHP教程 来源:网络整理
导读:我正在使用Codeigniter.2.1.3进行网站,所以我需要扩展CI_Controller,这样我就可以添加一个方法来执行所有控制器,所以我做了user_guide中的内容: 在application / core文件夹中创建一个名为MY_Controller.php的文件,在其中创建MY_Controller类扩展CI_Control
我正在使用Codeigniter.2.1.3进行网站,所以我需要扩展CI_Controller,这样我就可以添加一个方法来执行所有控制器,所以我做了user_guide中的内容:

在application / core文件夹中创建一个名为MY_Controller.php的文件,在其中创建MY_Controller类扩展CI_Controller,更改我的常规控制器以扩展MY_controller,如下所示:
MY_controller.php:

class MY_Controller extends CI_Controller{
    protected $page;
    # Constructor
    function __construct (){
        parent::__construct();
        #code shared with all controllers
    }
    public function get_page(){
        #code to get_the right page here
    }
}

常规控制器名为Regular.php:

class Regular extends MY_Controller{
     public function __construct(){
         parent::__construct();
     }
     public function index(){
          $this->get_page();
     }
}

但是出现以下错误:

Fatal error: Class ‘MY_Controller’ not found in /var/www/immo/CodeIgniter_2.1.3/application/controllers/regular.php on line 2

您需要包含MY_Controller类或自动加载它.我建议你通过在application / config / config.php文件中添加以下内容来自动加载它.
function __autoload($class)
{
    if (strpos($class,'CI_') !== 0)
    {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT))
        {
            include $file;
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读