2013-05-02 70 views
0

我有各种控制器core/文件夹名为core/my_controller.php和其他控制器libraries/文件夹为libraries/user_controller.php,​​。现在我使用下面的代码config.php来自动加载这些文件。但我不认为它的工作。Codeigniter自动加载控制器不工作

我看到此错误消息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 
{ 

工作,但如果我可以,我想知道使自动加载代码工作

回答

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/

+0

非常感谢。 但是,一旦我在我的linux服务器上做了同样的事情,并且我收到错误消息,因为我使用大写字母的文件名来匹配它们与型号名称 – prakashchhetri 2013-05-03 05:41:05

+0

不用担心!你是对的 - [模型文件名应该全部小写](http://ellislab.com/codeigniter/user-guide/general/models.html#anatomy)。检查用户指南中的命名惯例总是一个好主意。 – jleft 2013-05-03 07:51:15

2

还有autoloading non-ciclasses 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