找不到codeigniter MY_Controller [英] codeigniter MY_Controller not found

查看:146
本文介绍了找不到codeigniter MY_Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上使用Codeigniter.2.1.3,所以我需要扩展CI_Controller,以便可以添加要与所有控制器一起执行的方法,这样我就可以执行user_guide中的操作:

i’m using the Codeigniter.2.1.3 for a website, so i need to extend the CI_Controller so i can add a method to be executed with all controllers so i did what’s in the user_guide:

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

creating a file named MY_Controller.php in the application/core folder the creating in it MY_Controller Class that extends the CI_Controller, the changing my regular controller to extend the MY_controller like this: 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的常规控制器:

regular controller named Regular.php:

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

,但继续出现以下错误:

but the following error keep appearing:

致命错误:在第2行的/var/www/immo/CodeIgniter_2.1.3/application/controllers/regular.php中找不到类"MY_Controller"

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文件中来自动加载它.

You would need to include your MY_Controller class or auto-load it. I suggest you auto-load it by adding the following to your application/config/config.php file.

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

这篇关于找不到codeigniter MY_Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆