10

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.

3
  • 1
    I take it you are running the script from /var/www/mysite/ ? Commented Mar 4, 2013 at 19:15
  • Yes, otherwise I would immediately get a "Could not open input file" error. Commented Mar 5, 2013 at 10:07
  • I received this unhelpful error when there was a problem with connecting a postgres database with a docker container. I was unable to create a connection in PgAdmin. Upon reviewing the status of postgres in Docker, it was constantly spinning and never resolved (but Mongo did). Commented Apr 1 at 2:34

6 Answers 6

13

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.

Sign up to request clarification or add additional context in comments.

Comments

11

This error is often due to not being able to connect to the database. Yes, indeed, judging by the error message, they have nothing to do with each other. Nevertheless, this seems to be first visible error that comes up, when the framework is unable to connect to the database.

So, to solve the error "Class 'CI_Controller' not found", as a starting point, review your database settings (Application -> Config -> database.php), such as *hostname*, *username*, *password* and *database* and see if they are correct and if you can connect manually.

4 Comments

I understand, that you haven't had enough reputation to post comments yet, but this answer really looks like one. Try adding more details into it.
@sanjeev goel You are pointing us at right direction, but it takes me one hour to understand and figure out that whole thing. In my case problem is due to db driver of CI, since DB on local server was deleted by team developer so CI is giving this error. But obviously error from CI is not understandable.
right.. see you configuration database. database name, username and password must match.
Hi, this is true, please check you have the right database, the right username and password and if all this is correct then you can try to export your database, drop the existing one and create another one then import your db back, remember to change the name in database.php if you changed it in database
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.

1 Comment

For me it wasn't the Log class myking problems but the exception handler in Common.php So I had to change the loading of the Exceptions class accordingly: // $_error =& load_class('Exceptions', 'core'); require_once('system/core/Exceptions.php'); $_error = new CI_Exceptions('core');
0

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.

1 Comment

Thanks, in the link its using a PHP doc outside the CI application folder, and loads CI by including index.php. I need to run commands on a CI controller within my application. I did change my $config['uri_protocol'] = "PATH_INFO", but still have the same issue.
0

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.

Comments

-1

1 - Check disk space. Most of the time is caused for lower disk space. Clean up logs and free space.

2 - Restart the Mysql server (after cleaning space)

2 - downgrade your php version to 7.4

3 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
After all, the third point makes your answer pretty useless. No current application should use PHP 7.4 anymore
There will certainly be developers in the world who are stuck in PHP7.4 because their application is stuck in CI3.1.11 (at most). If you have one or more dependencies which are not compatible with more modern versions, you have to suffer outdated versions of the language and/or the framework. The CI3.1.11 & PHP7.4 ceiling is probably the point when most people move to Laravel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.