Codeigniter自动加载控制器无法正常工作

时间:2013-05-02 17:39:03

标签: php codeigniter

我有各种控制器core/文件夹core/my_controller.phplibraries/文件夹中的其他控制器libraries/user_controller.phplibraries/frontend_controller.php。现在我在config.php中使用以下代码来自动加载这些文件。但我认为它不起作用。

我看到此错误消息Fatal error: Class 'MY_Controller' not found in /home/manumakeadmin/manumake.com/2d/application/libraries/frontend_controller.php on line 3

function __autoload($classname) {

    if (strpos($classname, 'CI_') !== 0) {
        $file = APPPATH . 'libraries/' . $classname . '.php';
        if (file_exists($file) && is_file($file)) {
            @include_once($file);
        }
    }
}

修改

我可以通过手动将文件包含为

来使其工作
<?php
include_once APPPATH.'core/my_controller.php';

class Frontend_controller extends MY_Controller
{

但我想知道我是否可以让自动加载代码工作

2 个答案:

答案 0 :(得分:2)

此前两个链接和autoloading non-ci中还记录了classes technique mentioned

为config.php添加一个片段来加载这些类就可以了。

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

并在application/core/Base_Controller.php

中添加基类

答案 1 :(得分:1)

Library file names and class names must match and be capitalised - Frontend_controller.php

extending a core class时,文件名也必须与类名匹配。将前缀和类名的第一个字母大写在文件中:MY_Controller.php前缀可以设置为:application/config/config.php

还要确保您的文件位于application目录中,而不是system。这似乎是这种情况,但值得检查。

检查user guide命名约定总是一个好主意。例如,model类名必须首字母大写,其余小写;文件名应全部小写,并与类名匹配。


然而,实现libraries in CodeIgniter aren't intended for extending core classes非常重要,例如CI_Controller,我认为MY_Controller正在扩展。图书馆应该用于:

  
      
  • 创建全新的库。
  •   
  • 扩展本地库。
  •   
  • 替换原生图书馆。
  •   

我认为您的Frontend_controller可能会更好地位于:application/controllers/