找不到代码点火器 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();
     }
}

但不断出现以下错误:

致命错误:在第 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;
        }
    }
} 

相关文章