Codeigniter Command line error - PHP Fatal error:

2019-01-15 10:19发布

问题:

After following the user guide instructions found here: http://ellislab.com/codeigniter/user-guide/general/cli.html I'm unable to run the test script via command line.

My controller located at /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"

From terminal I should be able to run:

$ php index.php tools message "Ben"

My terminal should print: "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

My server is pretty standard; ubuntu LAMP. Codeigniter is pretty standard too and I have no problem running non CI scripts via command line

My PHP binary is only located in /usr/bin/php <-- This post suggests 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.

Any help or just an indication on how to debug this would be greatly appreciated.

Thanks in advance.

回答1:

Solved! (partly) the issue was CodeIgniters error logging.

In application/config/config.php, I modified the following config property:

$config['log_threshold'] = 0;

This disables logging, and allows $ php index.php to execute.

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.



回答2:

To solve error "Class 'CI_Controller' not found" try going to Application -> Config -> database.php then check the database name.



回答3:

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.

I worked around this issue by making the following changes to the Common.php file (hack, I know).

//$_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.



回答4:

This site says to run codeigniter from the command line, one must set the $_SERVER['PATH_INFO'] variable.

$_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.



回答5:

The answer provided in this Stack Overflow post worked for me.

Within system/core/CodeIgniter.php, on around line 75, change:

set_error_handler('_exception_handler');

to...

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.