As well as calling an applications Controllers via the URL in a browser they can also be loaded via the command-line interface (CLI).
Page Contents
The command-line interface is a text-based method of interacting with computers. For more information, check the Wikipedia article.
There are many reasons for running CodeIgniter from the command-line, but they are not always obvious.
is_cli()
.Let’s create a simple controller so you can see it in action. Using your text editor, create a file called Tools.php, and put the following code in it:
<?php class Tools extends CI_Controller { public function message($to = 'World') { echo "Hello {$to}!".PHP_EOL; } }
Then save the file to your application/controllers/ folder.
Now normally you would visit the site using a URL similar to this:
example.com/index.php/tools/message/to
Instead, we are going to open the terminal in Mac/Linux or go to Run > “cmd” in Windows and navigate to our CodeIgniter project.
$ cd /path/to/project; $ php index.php tools message
If you did it right, you should see Hello World! printed.
$ php index.php tools message "John Smith"
Here we are passing it a argument in the same way that URL parameters work. “John Smith” is passed as a argument and output is:
Hello John Smith!
That, in a nutshell, is all there is to know about controllers on the command line. Remember that this is just a normal controller, so routing and _remap()
works fine.
© 2014–2017 British Columbia Institute of Technology
Licensed under the MIT License.
https://www.codeigniter.com/user_guide/general/cli.html