Phil Sturgeon | 8d63cc6 | 2011-04-07 11:52:41 +0100 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 3 | <head> |
| 4 | |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 | <title>Running via the CLI : CodeIgniter User Guide</title> |
| 7 | |
| 8 | <style type='text/css' media='all'>@import url('../userguide.css');</style> |
| 9 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> |
| 10 | |
| 11 | <script type="text/javascript" src="../nav/nav.js"></script> |
| 12 | <script type="text/javascript" src="../nav/prototype.lite.js"></script> |
| 13 | <script type="text/javascript" src="../nav/moo.fx.js"></script> |
| 14 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script> |
| 15 | |
| 16 | <meta http-equiv='expires' content='-1' /> |
| 17 | <meta http-equiv= 'pragma' content='no-cache' /> |
| 18 | <meta name='robots' content='all' /> |
| 19 | <meta name='author' content='ExpressionEngine Dev Team' /> |
| 20 | <meta name='description' content='CodeIgniter User Guide' /> |
| 21 | |
| 22 | </head> |
| 23 | <body> |
| 24 | |
| 25 | <!-- START NAVIGATION --> |
| 26 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> |
| 27 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> |
| 28 | <div id="masthead"> |
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> |
| 30 | <tr> |
Derek Jones | b8c038a | 2011-08-20 08:57:14 -0500 | [diff] [blame^] | 31 | <td><h1>CodeIgniter User Guide Version 2.0.3</h1></td> |
Phil Sturgeon | 8d63cc6 | 2011-04-07 11:52:41 +0100 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> |
| 33 | </tr> |
| 34 | </table> |
| 35 | </div> |
| 36 | <!-- END NAVIGATION --> |
| 37 | |
| 38 | |
| 39 | <!-- START BREADCRUMB --> |
| 40 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> |
| 41 | <tr> |
| 42 | <td id="breadcrumb"> |
| 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> › |
| 44 | <a href="../index.html">User Guide Home</a> › |
| 45 | Running via the CLI |
| 46 | </td> |
| 47 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> |
| 48 | </tr> |
| 49 | </table> |
| 50 | <!-- END BREADCRUMB --> |
| 51 | |
| 52 | <br clear="all" /> |
| 53 | |
| 54 | |
| 55 | <!-- START CONTENT --> |
| 56 | <div id="content"> |
| 57 | |
| 58 | <h1>Running via the CLI</h1> |
| 59 | |
| 60 | <p> |
| 61 | As well as calling an applications <a href="./controllers.html">Controllers</a> via the URL in a browser they can also be loaded via the command-line interface (CLI). |
| 62 | </p> |
| 63 | |
| 64 | |
| 65 | <ul> |
| 66 | <li><a href="#what">What is the CLI?</a></li> |
| 67 | <li><a href="#why">Why use this method?</a></li> |
| 68 | <li><a href="#how">How does it work?</a></li> |
| 69 | </ul> |
| 70 | |
| 71 | |
| 72 | <a name="what"></a> |
| 73 | <h2>What is the CLI?</h2> |
| 74 | |
Anaxamaxan | 4941487 | 2011-05-12 15:24:00 -0700 | [diff] [blame] | 75 | <p><dfn>The command-line interface is a text-based method of interacting with computers.</dfn> For more information, check the <a href="http://en.wikipedia.org/wiki/Command-line_interface">Wikipedia article</a>.</p> |
Phil Sturgeon | 8d63cc6 | 2011-04-07 11:52:41 +0100 | [diff] [blame] | 76 | |
| 77 | <a name="why"></a> |
| 78 | |
| 79 | <h2>Why run via the command-line?</h2> |
| 80 | |
| 81 | <p> |
| 82 | There are many reasons for running CodeIgniter from the command-line, but they are not always obvious.</p> |
| 83 | |
| 84 | <ul> |
| 85 | <li>Run your cron-jobs without needing to use wget or curl</li> |
| 86 | <li>Make your cron-jobs inaccessible from being loaded in the URL by checking for <kbd>IS_CLI</kbd></li> |
| 87 | <li>Make interactive "tasks" that can do things like set permissions, prune cache folders, run backups, etc.</li> |
| 88 | <li>Integrate with other applications in other languages. For example, a random C++ script could call one command and run code in your models!</li> |
| 89 | </ul> |
| 90 | |
| 91 | <a name="how"></a> |
| 92 | <h2>Let's try it: Hello World!</h2> |
| 93 | |
| 94 | <p>Let's create a simple controller so you can see it in action. Using your text editor, create a file called <dfn>tools.php</dfn>, and put the following code in it:</p> |
| 95 | |
| 96 | <textarea class="textarea" style="width:100%" cols="50" rows="10"> |
| 97 | <?php |
| 98 | class Tools extends CI_Controller { |
| 99 | |
| 100 | public function message($to = 'World') |
| 101 | { |
| 102 | echo "Hello {$to}!".PHP_EOL; |
| 103 | } |
| 104 | } |
| 105 | ?> |
| 106 | </textarea> |
| 107 | |
| 108 | <p>Then save the file to your <dfn>application/controllers/</dfn> folder.</p> |
| 109 | |
| 110 | <p>Now normally you would visit the your site using a URL similar to this:</p> |
| 111 | |
| 112 | <code>example.com/index.php/<var>tools</var>/<var>message</var>/<var>to</var></code> |
| 113 | |
| 114 | <p>Instead, we are going to open Terminal in Mac/Lunix or go to Run > "cmd" in Windows and navigate to our CodeIgniter project.</p> |
| 115 | |
| 116 | <blockquote> |
| 117 | $ cd /path/to/project;<br/> |
| 118 | $ php index.php tools message |
| 119 | </blockquote> |
| 120 | |
| 121 | <p>If you did it right, you should see <samp>Hello World!</samp>.</p> |
| 122 | |
| 123 | <blockquote> |
| 124 | $ php index.php tools message "John Smith" |
| 125 | </blockquote> |
| 126 | |
| 127 | <p>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: <samp>Hello John Smith!</samp>.</p> |
| 128 | |
| 129 | <h2>That's it!</h2> |
| 130 | |
| 131 | <p>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.</p> |
| 132 | |
| 133 | |
| 134 | |
| 135 | </div> |
| 136 | <!-- END CONTENT --> |
| 137 | |
| 138 | |
| 139 | <div id="footer"> |
| 140 | <p> |
| 141 | Previous Topic: <a href="urls.html">CodeIgniter URLs</a> |
| 142 | · |
| 143 | <a href="#top">Top of Page</a> · |
| 144 | <a href="../index.html">User Guide Home</a> · |
| 145 | Next Topic: <a href="reserved_names.html">Reserved Names</a></p> |
| 146 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> |
| 147 | </div> |
| 148 | |
| 149 | </body> |
| 150 | </html> |