编码器自动加载控制器不工作


Codeigniter autoload controller not working

我有各种控制器是core/文件夹命名为core/my_controller.php, libraries/文件夹中的其他控制器为libraries/user_controller.php, libraries/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
{

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

在前面提到的两个链接中也有自动加载非ci类的技术。

在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

库文件名和类名必须匹配并大写- Frontend_controller.php

当扩展核心类时,文件名也必须与类名匹配。将前缀和文件中类名的第一个字母大写:MY_Controller.php前缀可以设置为:application/config/config.php

还要确保文件位于application目录中,而不是system目录中。似乎是这样,但值得检查一下。

检查用户指南中的命名约定总是一个好主意。例如,模型类名必须首字母大写,其余字母小写;文件名应该是小写的,并且与类名匹配。


然而,意识到CodeIgniter中的库不是用于扩展核心类的,例如CI_Controller,我认为MY_Controller正在扩展,这一点非常重要。库应该用于:

  • 创建全新的库
  • 扩展本地库。
  • 替换本地库。

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