blob: 649d5d548792669ed414ef17446ae0d897eef615 [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
24- Run your cron-jobs without needing to use wget or curl
25- Make your cron-jobs inaccessible from being loaded in the URL by
26 checking for ``$this->input->is_cli_request()``
27- 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
36text 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 }
47 ?>
48
Derek Jones8ede1a22011-10-05 13:34:52 -050049Then save the file to your application/controllers/ folder.
50
51Now normally you would visit the your site using a URL similar to this::
52
53 example.com/index.php/tools/message/to
54
Erocanti0130ace2012-10-09 02:53:58 -050055Instead, we are going to open Terminal in Mac/Linux or go to Run > "cmd"
Derek Jones8ede1a22011-10-05 13:34:52 -050056in Windows and navigate to our CodeIgniter project.
57
Derek Jones46715e52011-10-05 17:36:22 -050058.. code-block:: bash
59
Derek Jones8ede1a22011-10-05 13:34:52 -050060 $ cd /path/to/project;
61 $ php index.php tools message
62
63If you did it right, you should see Hello World!.
64
Derek Jones46715e52011-10-05 17:36:22 -050065.. code-block:: bash
66
Derek Jones8ede1a22011-10-05 13:34:52 -050067 $ php index.php tools message "John Smith"
68
69Here we are passing it a argument in the same way that URL parameters
70work. "John Smith" is passed as a argument and output is: Hello John
71Smith!.
72
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
78and _remap works fine.