11

I'm following this tutorial on running Codeigniter via the CLI. I've done everything they've done (copied and pasted) now when I run this command, it doesn't do what is expected except it outputs the website index contents.

$ cd /Users/MyUsername/Sites/code
$ php index.php tools message

The output I get is the index page HTML source, e.g. http://localhost/code.

The expected result should be

Hello World!

How can I achieve this to make it work?

hakre
  • 193,403
  • 52
  • 435
  • 836
MacMac
  • 34,294
  • 55
  • 151
  • 222

6 Answers6

9

try this:

php index.php/controller/function/param1/param2/param3 etc

or

php index.php controller function param1 param2 param3 etc

also, post the content of your $_SERVER var and value of the 'uri_protocol' variable from config.php.

$config['uri_protocol'] should be 'AUTO'

Muhammad Fahad
  • 1,352
  • 15
  • 15
Vlad Balmos
  • 3,372
  • 19
  • 34
  • 5
    I have the similar problem, but when I use a `php index.php/tools/message` I have `No input file specified.` error – Mikhail Jun 07 '12 at 16:05
  • Mikhail, I'm having the same issue as you. Did you find a solution? – Jonny White Jul 20 '13 at 08:39
  • `php index.php/tools/message` not working when I run `docker exec`, I have to change $config['uri_protocol'] to be 'AUTO'. – zx1986 Jan 28 '16 at 07:26
  • If you try to run Codeigniter via CLI **on shared web hosting with CRON**, the php command itself may be the problem. I wrote a CRON line on cPanel, but the index.php script didn't get the arguments. Finally it worked with the PATH_INFO option and with arguments separated with spaces, but I had to use the command **`/usr/local/bin/php`** instead of `/usr/bin/php` – Márton Tamás Oct 21 '16 at 16:46
5

For me this was resolved by changing:

$config['uri_protocol'] = 'AUTO' 

in the main config file.

I had previously changed it to

'REQUEST_URI'

which doesn't work with CLI apparently.

emartel
  • 7,712
  • 1
  • 30
  • 58
  • Was trying to run from cli with uri_protocol set to "PATH_INFO" didn't work then, changing it to "AUTO" did it for me. thanks! – Jaspal Singh Jul 24 '13 at 15:07
1

If you use wget in conjunction with $this->input->ip_address(); you can ensure that the only machine accessing your script is your own webserver. It isn't as good as being able to call the file locally using php index.php controller function, but it is a fallback.

Here's what I have created which has worked for a few months without issue:

  1. Create a directory somewhere on your server that you can just use as a scratch pad for the temporary files created by wget. I created a folder named cron one level below my public_html folder.

    ex. /home/myuser/cron

  2. Construct your cron command. You can string together commands using &&.

    i. cd /home/myuser/cron && -- move to your scratch directory

    ii. wget http://www.example.com/cron/foo && -- wget your file

    iii. rm -f foo -- Remove the downloaded file "foo" from your scratch directory

  3. Your final command will look something like this:

    cd /home/myuser/cron && wget http://www.example.com/cron/foo && rm -f foo

  4. Check that the IP address accessing your cron files equals your webserver's IP:

     <?php defined('BASEPATH') OR exit('No direct script access allowed');
    
     class Cron extends MY_Controller {
    
         public function __construct()
         {
    
             parent::__construct();
    
             // this controller can only be called from the IP xxx.xxx.xxx.xxx
             if ($this->input->ip_address() !== 'xxx.xxx.xxx.xxx'){
    
                 show_error('Direct access is not allowed');
    
             }
    
         }
    
         function foo($bar = 'bar')
         {
    
             echo $this->input->ip_address();
    
         }
    
     }
    

Important: Please make sure that you fully understand the effects of rm -f. It can have interesting consequences if you do not supply the correct file. If you have spare time, you could opt out of removing the file and just manually remove all of the cron scratch files periodically.

miken32
  • 42,008
  • 16
  • 111
  • 154
VictorKilo
  • 1,839
  • 3
  • 24
  • 39
  • 1
    Yeah, that's great for people who want to use `wget`. Please note you can use `--delete-after` to delete the downloaded page after running `wget`. – MacMac May 20 '13 at 19:25
  • hmm, I was not aware of that. I should've read the man page first. That is probably better than using rm. – VictorKilo May 21 '13 at 08:40
1

The output I get is the index page HTML source, e.g. http://localhost/code.

You have got an error with your routing, your new example CLI controller is not fired, but another controller. Which error you have with your routing is hard to say, as you have not shown your whole code.

hakre
  • 193,403
  • 52
  • 435
  • 836
0

This worked for me - found at http://www.daharveyjr.com/codeigniter-cli-command-line-interface/

$config['uri_protocol'] = 'CLI';

I then run like this

php index.php controller/function/parameter
0

Ok sorry to say but here on this page https://ellislab.com/codeigniter/user-guide/general/cli.html

They didn't mention whether the root path of the folder where we place the codeigniter or the controllers folder. I searched a lot over that issue. I tried different things and then I found that.

before that set PHP to your PATH environment variables {Run php file in windows CMD}

after setting the PATH environment variables

  1. go to cmd
  2. cd the path where you put codeigniter (i.e CI/)
  3. php index.php/tools/message

you will get

Hello World!

on to your cmd screen.

Community
  • 1
  • 1
khalrd
  • 60
  • 6