Codeigniter 命令行错误 - PHP 致命错误:找不到类“CI_Controller”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15207937/
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 Command line error - PHP Fatal error: Class 'CI_Controller' not found
提问by Mijahn
After following the user guide instructions found here: http://ellislab.com/codeigniter/user-guide/general/cli.htmlI'm unable to run the test script via command line.
按照此处找到的用户指南说明进行操作后:http: //ellislab.com/codeigniter/user-guide/general/cli.html我无法通过命令行运行测试脚本。
My controller located at /var/www/mysite/application/controllers/
我的控制器位于 /var/www/mysite/application/controllers/
class Tools extends CI_Controller {
public function message($to = 'World')
{
echo "Hello {$to}!".PHP_EOL;
}
}
In my browser I can access
在我的浏览器中,我可以访问
http://mysite/tools/message/ben
And the function correctly outputs "Hello ben"
并且该函数正确输出“Hello ben”
From terminal I should be able to run:
从终端我应该能够运行:
$ php index.php tools message "Ben"
$ php index.php 工具消息“奔”
My terminal should print: "Hello Ben"
我的终端应该打印:“Hello Ben”
However I get the following error:
但是我收到以下错误:
PHP Fatal error: Class 'CI_Controller' not found in /var/www/mysite/system/core/CodeIgniter.php on line 233
PHP 致命错误:在第 233 行的 /var/www/mysite/system/core/CodeIgniter.php 中找不到类“CI_Controller”
My server is pretty standard; ubuntu LAMP. Codeigniter is pretty standard too and I have no problem running non CI scripts via command line
我的服务器非常标准;Ubuntu 灯。Codeigniter 也很标准,我通过命令行运行非 CI 脚本没有问题
My PHP binary is only located in /usr/bin/php <-- This postsuggests an issue running CI directly from usr/bin/php, however I'm not operating a shared PHP service, and I don't see why this would make a difference to how PHP executes a CI script.
我的 PHP 二进制文件仅位于 /usr/bin/php <--这篇文章提出了直接从 usr/bin/php 运行 CI 的问题,但是我没有运行共享的 PHP 服务,我不明白为什么会这样将对 PHP 执行 CI 脚本的方式产生影响。
Any help or just an indication on how to debug this would be greatly appreciated.
任何帮助或只是有关如何调试的指示将不胜感激。
Thanks in advance.
提前致谢。
回答by Mijahn
Solved! (partly) the issue was CodeIgniters error logging.
解决了!(部分)问题是 CodeIgniters 错误日志记录。
In application/config/config.php, I modified the following config property:
在 application/config/config.php 中,我修改了以下配置属性:
$config['log_threshold'] = 0;
This disables logging, and allows $ php index.phpto execute.
这将禁用日志记录,并允许$ php index.php执行。
If anyone can explain why CI only shows this error on CLI PHP - might help anyone else who has this issue and needs it resolved with error logging on.
如果有人可以解释为什么 CI 仅在 CLI PHP 上显示此错误 - 可能会帮助其他遇到此问题并需要通过错误登录解决问题的人。
回答by sanjeev goel
To solve error "Class 'CI_Controller' not found" try going to Application -> Config -> database.phpthen check the database details like hostname, username, passwordand database.
要解决错误“找不到类‘CI_Controller’”,请尝试Application -> Config -> database.php检查数据库详细信息,如主机名、用户名、密码和数据库。
回答by dxhans5
To Mijahn:
致米詹:
I had this same problem, and after about two hours of tracing through code to figure out the problem, it seems that there is some sort of conflict with loading the CI_Controller when utilizing the native PHP load_class function.
我遇到了同样的问题,经过大约两个小时的代码跟踪以找出问题后,似乎在使用本机 PHP load_class 函数时加载 CI_Controller 存在某种冲突。
I worked around this issue by making the following changes to the Common.php file (hack, I know).
我通过对 Common.php 文件进行以下更改来解决这个问题(hack,我知道)。
//$_log =& load_class('Log');
require_once('system/libraries/Log.php');
$_log = new CI_Log();
My logs then where created exactly like I wanted. Hope this hack helps.
我的日志然后按照我想要的方式创建。希望这个黑客有帮助。
回答by Motes
This sitesays to run codeigniter from the command line, one must set the $_SERVER['PATH_INFO'] variable.
该站点说要从命令行运行 codeigniter,必须设置 $_SERVER['PATH_INFO'] 变量。
$_SERVER['PATH_INFO'] is usually supplied by php when a web request is made. However, since we are calling this script from the command line, we need to emulate this small part of the environment as a web request.
$_SERVER['PATH_INFO'] 通常在发出 Web 请求时由 php 提供。但是,由于我们是从命令行调用此脚本,因此我们需要将环境的这一小部分模拟为 Web 请求。
回答by sevenpointsix
The answer provided in this Stack Overflow postworked for me.
此 Stack Overflow 帖子中提供的答案对我有用。
Within system/core/CodeIgniter.php, on around line 75, change:
在system/core/CodeIgniter.php75 行左右,更改:
set_error_handler('_exception_handler');
set_error_handler('_exception_handler');
to...
到...
set_exception_handler('_exception_handler');
set_exception_handler('_exception_handler');
Other users have reported that this gave them a better backtrace with which to debug the underlying issue, but for me, this actually removed the problem altogether.
其他用户报告说,这为他们提供了更好的回溯来调试潜在问题,但对我来说,这实际上完全消除了问题。

