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">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <title>Running via the CLI : CodeIgniter User Guide</title>
8 <style type='text/css' media='all'>@import url('../userguide.css');</style>
9 <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
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>
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' />
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>
29 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
31 <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
32 <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
36 <!-- END NAVIGATION -->
39 <!-- START BREADCRUMB -->
40 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
43 <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
44 <a href="../index.html">User Guide Home</a> ›
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>
50 <!-- END BREADCRUMB -->
55 <!-- START CONTENT -->
58 <h1>Running via the CLI</h1>
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).
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>
73 <h2>What is the CLI?</h2>
75 <p><dfn>The command-line interface is a text-based method of interacting with computers that looks like what most people remember as DOS.</dfn></p>
79 <h2>Why run via the command-line?</h2>
82 There are many reasons for running CodeIgniter from the command-line, but they are not always obvious.</p>
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>
92 <h2>Let's try it: Hello World!</h2>
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>
96 <textarea class="textarea" style="width:100%" cols="50" rows="10">
98 class Tools extends CI_Controller {
100 public function message($to = 'World')
102 echo "Hello {$to}!".PHP_EOL;
108 <p>Then save the file to your <dfn>application/controllers/</dfn> folder.</p>
110 <p>Now normally you would visit the your site using a URL similar to this:</p>
112 <code>example.com/index.php/<var>tools</var>/<var>message</var>/<var>to</var></code>
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>
117 $ cd /path/to/project;<br/>
118 $ php index.php tools message
121 <p>If you did it right, you should see <samp>Hello World!</samp>.</p>
124 $ php index.php tools message "John Smith"
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>
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>
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>