[英]Codeigniter autoload controller not working
I have various controller is core/
folder named core/my_controller.php
and other controllers in libraries/
folder as libraries/user_controller.php
, libraries/frontend_controller.php
. 我有各种控制器,它们是
core/
文件夹,名为core/my_controller.php
而libraries/
文件夹中的其他控制器分别为libraries/user_controller.php
, libraries/frontend_controller.php
。 Now I am using this code below in config.php
to autoload these files. 现在,我在
config.php
使用以下代码自动加载这些文件。 But I dont think its working. 但是我不认为它起作用。
I am seeing this error message Fatal error: Class 'MY_Controller' not found in /home/manumakeadmin/manumake.com/2d/application/libraries/frontend_controller.php on line 3
我看到此错误消息
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);
}
}
}
EDIT 编辑
I can make it work by manually including the files as 我可以通过手动添加文件来使其工作
<?php
include_once APPPATH.'core/my_controller.php';
class Frontend_controller extends MY_Controller
{
But I was wondering if I could make the autoload code to work 但是我想知道是否可以使自动加载代码正常工作
There is also autoloading non-ci classes technique documented in those two links and mentioned before. 在这两个链接中也记录了自动加载非ci 类技术 ,并在前面提到过。
Adding an snippet to config.php to load those classes does the trick. 向config.php添加一个代码片段以加载这些类就可以了。
function __autoload($class)
{
if (substr($class,0,3) !== 'CI_')
{
if (file_exists($file = APPPATH . 'core/' . $class . EXT))
{
include $file;
}
}
}
and adding your base class in application/core/Base_Controller.php
并在
application/core/Base_Controller.php
添加您的基类
Library file names and class names must match and be capitalised - Frontend_controller.php
库文件名和类名必须匹配并大写
Frontend_controller.php
When extending a core class , the file name must also match the class name. 扩展核心类时 ,文件名也必须与类名匹配。 Capitalise the prefix and the first letter of the class name in the file:
MY_Controller.php
The prefix can be set in: application/config/config.php
将前缀和类名的首字母大写在文件中:
MY_Controller.php
可以在以下位置设置前缀: application/config/config.php
Also ensure that your files are in the application
directory, rather than system
. 还要确保您的文件位于
application
目录中,而不是system
。 This seems to be the case, but it's worth checking. 似乎是这种情况,但值得检查。
It's always a good idea to check the user guide for naming conventions. 查看用户指南中的命名约定始终是一个好主意。 For example, model class names must have the first letter capitalised and the rest lowercase;
例如, 模型类名称的首字母必须大写,其余小写; the file name should be all lower case and match the class name.
文件名应全部小写并与类名匹配。
However, it's very important to realise that libraries in CodeIgniter aren't intended for extending core classes , for example CI_Controller
, which I assume MY_Controller
is extending. 但是,认识到CodeIgniter中的库不是要扩展核心类 (例如
CI_Controller
,我假设我要扩展MY_Controller
是非常重要的。 Libraries should be used to: 库应用于:
- create entirely new libraries.
创建全新的库。
- extend native libraries.
扩展本机库。
- replace native libraries.
替换本机库。
I think it's likely that your Frontend_controller
would be better located in: application/controllers/
我认为您的
Frontend_controller
可能会更好地位于: application/controllers/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.