php Codeigniter 扩展控制器,未找到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21351808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter extending controller, controller not found
提问by Harlsten
In Codeigniter 2.1.2 I want to create base controller and then extends from this controller. It does not work and I have no idea why and I'm pretty desperate now.
在 Codeigniter 2.1.2 中,我想创建基本控制器,然后从该控制器扩展。它不起作用,我不知道为什么,我现在非常绝望。
In \application\core\MY_Base_Controller.phpI have this:
在\application\core\MY_Base_Controller.php我有这个:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Base_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
...
In \application\controllers\Home.phpI have this:
在\application\controllers\Home.php我有这个:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Base_Controller {
And the error message is
错误信息是
Fatal error: Class 'MY_Base_Controller' not found in ...\application\controllers\Home.php on line 3
致命错误:在第 3 行的 ...\application\controllers\Home.php 中找不到“MY_Base_Controller”类
I have no idea what to do, I read all over the internet that I have to put base controller to core folder what I did, that I have to name base controller with prefix MY_, I did. But it is still no working. And in my config.php is this line as well
我不知道该怎么做,我在互联网上读到我必须将基本控制器放入核心文件夹我所做的,我必须用前缀 MY_ 命名基本控制器,我做到了。但它仍然没有工作。在我的 config.php 中也是这一行
$config['subclass_prefix'] = 'MY_';
Im running this on localhost using xampp
我使用 xampp 在本地主机上运行它
Thank you for your help
感谢您的帮助
EDIT
编辑
Could someone please downlod following link try it, and tell me whats wrong. I have just downloaded codeigniter tried to create base controller and extend welcome controller. Not working. In following rar there are just modified files. Thank you http://goo.gl/sKHkDl
有人可以下载以下链接试试看,然后告诉我出了什么问题。我刚刚下载了 codeigniter 试图创建基本控制器并扩展欢迎控制器。不工作。在下面的 rar 中只有修改过的文件。谢谢 http://goo.gl/sKHkDl
EDIT 2
编辑 2
I'm able to get this working by renaming MY_Base_Controllerto MY_Controller. Does this mean, I'm able to create only one extended class for a controller ? eg. I can not have
我可以通过将MY_Base_Controller重命名为MY_Controller来使其工作。这是否意味着,我只能为控制器创建一个扩展类?例如。我不能有
- MY_Base_Auth_Controller
- MY_Base_Article_Controller
- MY_Base_Auth_Controller
- MY_Base_Article_Controller
Just and only MY_Controller?
只是MY_Controller?
采纳答案by Jose
I had the same problem, but if I created all controllers in the MY_Controller.php file all worked well.
我遇到了同样的问题,但如果我在 MY_Controller.php 文件中创建了所有控制器,一切都运行良好。
<?php
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
// do some stuff
}
}
class MY_Auth_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
// check if logged_in
}
}
回答by Websmith
I've had the same issue in my first CI application and found two key elements that need to be checked:
我在我的第一个 CI 应用程序中遇到了同样的问题,并发现了两个需要检查的关键元素:
1. Case Matching:Depending on your server configuration, the name of your file in the directory will need to match the case of your class. For instance, where your class is called "MY_Controller" your file name will need to be: "MY_Controller.php" on a Linux server. Windows servers have been known to have issues with uppercase filenames so you might experiment naming your controller "my_controller.php" and/or changing the extension to "my_" in your config.php instead of "MY_"
1.大小写匹配:根据您的服务器配置,目录中文件的名称需要与您的类的大小写匹配。例如,如果您的类名为“MY_Controller”,则您的文件名需要为:Linux 服务器上的“MY_Controller.php”。众所周知,Windows 服务器存在大写文件名的问题,因此您可以尝试将控制器命名为“my_controller.php”和/或在 config.php 中将扩展名更改为“my_”而不是“MY_”
2. Insert an Autoloading functionFor reasons unknown to me, Codeigniter does not automatically recognize and read extended core classes before first loading the core class. This can cause issues with your extension not loading in correctly. To remedy this, you can add this simple autoloading script to the very bottom of your config.php
2.插入一个自动加载函数由于我不知道的原因,Codeigniter在第一次加载核心类之前不会自动识别和读取扩展的核心类。这可能会导致您的扩展程序无法正确加载。为了解决这个问题,您可以将这个简单的自动加载脚本添加到 config.php 的最底部
/*
|--------------------------------------------------------------------------
| Autoload Custom Controllers
|--------------------------------------------------------------------------
|
*/
function __autoload($class) {
if (substr($class,0,3) !== 'CI_') {
if (file_exists($file = APPPATH . 'core/' . $class . EXT)) {
include $file;
}
}
}
Side note:the solution above was tested on CodeIgniter 2.1.4. The question asked involved CodeIgniter 2.1.2
旁注:上述解决方案在 CodeIgniter 2.1.4 上进行了测试。问的问题涉及 CodeIgniter 2.1.2
回答by Brian Ramsey
Anyone reading this using CI 3+ and trying to attempt the same thing. Please note that the global EXT was depreciated when dropping php 4 support. You need to use the following now:
任何使用 CI 3+ 阅读本文并尝试尝试相同操作的人。请注意,在放弃 php 4 支持时,全局 EXT 已贬值。您现在需要使用以下内容:
/*
|--------------------------------------------------------------------------
| Autoload Custom Controllers
|--------------------------------------------------------------------------
|
*/
function __autoload($class) {
if (substr($class,0,3) !== 'CI_') {
if (file_exists($file = APPPATH . 'core/' . $class . '.php')) {
include $file;
}
}
}
回答by Alexander Yagovdik
Yes, with core's MY_ classes you can override ONLY codeigniter framework entities such as Controller, Model, Config, Exception and so on. Please refer to https://ellislab.com/codeigniter/user-guide/general/core_classes.html
是的,使用 core 的 MY_ 类,您只能覆盖 codeigniter 框架实体,例如 Controller、Model、Config、Exception 等。请参考https://ellislab.com/codeigniter/user-guide/general/core_classes.html
回答by Purushottam zende
I faced same issue. I think the problem is, CI only loading only single file which must be named as "MY_controller.php". (system/core/Codeigniter.php - line 238). It is made to load only "MY_Controller.php". Answers made above like "_autoload" are there because we are overriding that functionality. I guess its in both i.e CI -2 and CI - 3. Either we can make changes at core files or we can use autload function, as mentioned above.
我遇到了同样的问题。我认为问题是,CI 只加载必须命名为“MY_controller.php”的单个文件。(system/core/Codeigniter.php - 第 238 行)。它仅用于加载“MY_Controller.php”。上面像“_autoload”这样的答案是存在的,因为我们正在覆盖该功能。我猜它在 CI -2 和 CI - 3 中都可以。我们可以在核心文件中进行更改,或者我们可以使用自动加载功能,如上所述。
thanks,
谢谢,

![php 致命错误:未捕获的 SoapFault 异常:[WSDL] SOAP-ERROR:解析 WSDL:无法加载](/res/img/loading.gif)