Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################### |
| 2 | Running via the CLI |
| 3 | ################### |
| 4 | |
| 5 | As well as calling an applications :doc:`Controllers <./controllers>` |
| 6 | via the URL in a browser they can also be loaded via the command-line |
| 7 | interface (CLI). |
| 8 | |
Joseph Wensley | 5b3ea1a | 2011-10-06 20:54:32 -0400 | [diff] [blame] | 9 | .. contents:: Page Contents |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 10 | |
| 11 | What is the CLI? |
| 12 | ================ |
| 13 | |
| 14 | The command-line interface is a text-based method of interacting with |
| 15 | computers. For more information, check the `Wikipedia |
| 16 | article <http://en.wikipedia.org/wiki/Command-line_interface>`_. |
| 17 | |
| 18 | Why run via the command-line? |
| 19 | ============================= |
| 20 | |
| 21 | There are many reasons for running CodeIgniter from the command-line, |
| 22 | but they are not always obvious. |
| 23 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 24 | - Run your cron-jobs without needing to use *wget* or *curl* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 25 | - Make your cron-jobs inaccessible from being loaded in the URL by |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 26 | checking the return value of :php:func:`is_cli()`. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
| 32 | Let's try it: Hello World! |
| 33 | ========================== |
| 34 | |
| 35 | Let's create a simple controller so you can see it in action. Using your |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 36 | text editor, create a file called Tools.php, and put the following code |
Derek Jones | 46715e5 | 2011-10-05 17:36:22 -0500 | [diff] [blame] | 37 | in it:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | |
Derek Jones | 46715e5 | 2011-10-05 17:36:22 -0500 | [diff] [blame] | 39 | <?php |
| 40 | class Tools extends CI_Controller { |
| 41 | |
| 42 | public function message($to = 'World') |
| 43 | { |
| 44 | echo "Hello {$to}!".PHP_EOL; |
| 45 | } |
| 46 | } |
Derek Jones | 46715e5 | 2011-10-05 17:36:22 -0500 | [diff] [blame] | 47 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 48 | Then save the file to your *application/controllers/* folder. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 49 | |
Andrey Andreev | c34a3d6 | 2016-10-14 14:23:10 +0300 | [diff] [blame] | 50 | Now normally you would visit the site using a URL similar to this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | |
| 52 | example.com/index.php/tools/message/to |
| 53 | |
Andrey Andreev | c34a3d6 | 2016-10-14 14:23:10 +0300 | [diff] [blame] | 54 | Instead, we are going to open the terminal in Mac/Linux or go to Run > "cmd" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 55 | in Windows and navigate to our CodeIgniter project. |
| 56 | |
Derek Jones | 46715e5 | 2011-10-05 17:36:22 -0500 | [diff] [blame] | 57 | .. code-block:: bash |
| 58 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 59 | $ cd /path/to/project; |
| 60 | $ php index.php tools message |
| 61 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 62 | If you did it right, you should see *Hello World!* printed. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 63 | |
Derek Jones | 46715e5 | 2011-10-05 17:36:22 -0500 | [diff] [blame] | 64 | .. code-block:: bash |
| 65 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | $ php index.php tools message "John Smith" |
| 67 | |
Andrey Andreev | 9a2ffb3 | 2018-08-22 14:44:51 +0300 | [diff] [blame^] | 68 | Here we are passing it an argument in the same way that URL parameters |
| 69 | work. "John Smith" is passed as an argument and the output is:: |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 70 | |
| 71 | Hello John Smith! |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | |
| 73 | That's it! |
| 74 | ========== |
| 75 | |
| 76 | That, in a nutshell, is all there is to know about controllers on the |
| 77 | command line. Remember that this is just a normal controller, so routing |
Andrey Andreev | c34a3d6 | 2016-10-14 14:23:10 +0300 | [diff] [blame] | 78 | and ``_remap()`` works fine. |