blob: bfa30531b46a72c504d877dd1ed49ca78aff3551 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###################
2Running via the CLI
3###################
4
5As well as calling an applications :doc:`Controllers <./controllers>`
6via the URL in a browser they can also be loaded via the command-line
7interface (CLI).
8
Joseph Wensley5b3ea1a2011-10-06 20:54:32 -04009.. contents:: Page Contents
Derek Jones8ede1a22011-10-05 13:34:52 -050010
11What is the CLI?
12================
13
14The command-line interface is a text-based method of interacting with
15computers. For more information, check the `Wikipedia
16article <http://en.wikipedia.org/wiki/Command-line_interface>`_.
17
18Why run via the command-line?
19=============================
20
21There are many reasons for running CodeIgniter from the command-line,
22but they are not always obvious.
23
Andrey Andreev16a704c2012-11-09 17:25:00 +020024- Run your cron-jobs without needing to use *wget* or *curl*
Derek Jones8ede1a22011-10-05 13:34:52 -050025- Make your cron-jobs inaccessible from being loaded in the URL by
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020026 checking the return value of :php:func:`is_cli()`.
Derek Jones8ede1a22011-10-05 13:34:52 -050027- Make interactive "tasks" that can do things like set permissions,
28 prune cache folders, run backups, etc.
29- Integrate with other applications in other languages. For example, a
30 random C++ script could call one command and run code in your models!
31
32Let's try it: Hello World!
33==========================
34
35Let's create a simple controller so you can see it in action. Using your
Andrey Andreev20292312013-07-22 14:29:10 +030036text editor, create a file called Tools.php, and put the following code
Derek Jones46715e52011-10-05 17:36:22 -050037in it::
Derek Jones8ede1a22011-10-05 13:34:52 -050038
Derek Jones46715e52011-10-05 17:36:22 -050039 <?php
40 class Tools extends CI_Controller {
41
42 public function message($to = 'World')
43 {
44 echo "Hello {$to}!".PHP_EOL;
45 }
46 }
Derek Jones46715e52011-10-05 17:36:22 -050047
Andrey Andreev16a704c2012-11-09 17:25:00 +020048Then save the file to your *application/controllers/* folder.
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Andrey Andreevc34a3d62016-10-14 14:23:10 +030050Now normally you would visit the site using a URL similar to this::
Derek Jones8ede1a22011-10-05 13:34:52 -050051
52 example.com/index.php/tools/message/to
53
Andrey Andreevc34a3d62016-10-14 14:23:10 +030054Instead, we are going to open the terminal in Mac/Linux or go to Run > "cmd"
Derek Jones8ede1a22011-10-05 13:34:52 -050055in Windows and navigate to our CodeIgniter project.
56
Derek Jones46715e52011-10-05 17:36:22 -050057.. code-block:: bash
58
Derek Jones8ede1a22011-10-05 13:34:52 -050059 $ cd /path/to/project;
60 $ php index.php tools message
61
Andrey Andreev16a704c2012-11-09 17:25:00 +020062If you did it right, you should see *Hello World!* printed.
Derek Jones8ede1a22011-10-05 13:34:52 -050063
Derek Jones46715e52011-10-05 17:36:22 -050064.. code-block:: bash
65
Derek Jones8ede1a22011-10-05 13:34:52 -050066 $ php index.php tools message "John Smith"
67
Andrey Andreev9a2ffb32018-08-22 14:44:51 +030068Here we are passing it an argument in the same way that URL parameters
69work. "John Smith" is passed as an argument and the output is::
Andrey Andreev16a704c2012-11-09 17:25:00 +020070
71 Hello John Smith!
Derek Jones8ede1a22011-10-05 13:34:52 -050072
73That's it!
74==========
75
76That, in a nutshell, is all there is to know about controllers on the
77command line. Remember that this is just a normal controller, so routing
Andrey Andreevc34a3d62016-10-14 14:23:10 +030078and ``_remap()`` works fine.