blob: bcd0e4bcea82386b90e7fca9cd537e620f7c9083 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<!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>Controllers : 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 Jones8917af72010-03-05 12:41:45 -060031<td><h1>CodeIgniter User Guide Version 2.0.0</h1></td>
Derek Allard2067d1a2008-11-13 22:59:24 +000032<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> &nbsp;&#8250;&nbsp;
44<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Controllers
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&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<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>Controllers</h1>
59
60<p>Controllers are the heart of your application, as they determine how HTTP requests should be handled.</p>
61
62
63<ul>
64<li><a href="#what">What is a Controller?</a></li>
65<li><a href="#hello">Hello World</a></li>
66<li><a href="#functions">Functions</a></li>
67<li><a href="#passinguri">Passing URI Segments to Your Functions</a></li>
68<li><a href="#default">Defining a Default Controller</a></li>
69<li><a href="#remapping">Remapping Function Calls</a></li>
70<li><a href="#output">Controlling Output Data</a></li>
71<li><a href="#private">Private Functions</a></li>
72<li><a href="#subfolders">Organizing Controllers into Sub-folders</a></li>
73<li><a href="#constructors">Class Constructors</a></li>
74<li><a href="#reserved">Reserved Function Names</a></li>
75</ul>
76
77
78<a name="what"></a>
79<h2>What is a Controller?</h2>
80
81<p><dfn>A Controller is simply a class file that is named in a way that can be associated with a URI.</dfn></p>
82
83<p>Consider this URI:</p>
84
85<code>example.com/index.php/<var>blog</var>/</code>
86
87<p>In the above example, CodeIgniter would attempt to find a controller named <dfn>blog.php</dfn> and load it.</p>
88
89<p><strong>When a controller's name matches the first segment of a URI, it will be loaded.</strong></p>
90
91<a name="hello"></a>
92<h2>Let's try it:&nbsp; 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>blog.php</dfn>, and put the following code in it:</p>
95
96
97<textarea class="textarea" style="width:100%" cols="50" rows="10">
Derek Allard4b6d4932008-12-07 17:04:56 +000098&lt;?php
Greg Aker63277b82010-11-09 13:46:13 -060099class Blog extends CI_Controller {
Derek Allard2067d1a2008-11-13 22:59:24 +0000100
101 function index()
102 {
103 echo 'Hello World!';
104 }
105}
Derek Allard4b6d4932008-12-07 17:04:56 +0000106?&gt;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107</textarea>
108
109
110
111<p>Then save the file to your <dfn>application/controllers/</dfn> folder.</p>
112
113<p>Now visit the your site using a URL similar to this:</p>
114
115<code>example.com/index.php/<var>blog</var>/</code>
116
117<p>If you did it right, you should see <samp>Hello World!</samp>.</p>
118
119<p>Note: Class names must start with an uppercase letter. In other words, this is valid:</p>
120
121<code>&lt;?php<br />
Greg Aker63277b82010-11-09 13:46:13 -0600122class <var>Blog</var> extends CI_Controller {<br />
Derek Allard2067d1a2008-11-13 22:59:24 +0000123<br />
124}<br />
125?&gt;</code>
126
127<p>This is <strong>not</strong> valid:</p>
128
129<code>&lt;?php<br />
Greg Aker63277b82010-11-09 13:46:13 -0600130class <var>blog</var> extends CI_Controller {<br />
Derek Allard2067d1a2008-11-13 22:59:24 +0000131<br />
132}<br />
133?&gt;</code>
134
135<p>Also, always make sure your controller <dfn>extends</dfn> the parent controller class so that it can inherit all its functions.</p>
136
137
138
139<a name="functions"></a>
140<h2>Functions</h2>
141
142<p>In the above example the function name is <dfn>index()</dfn>. The "index" function is always loaded by default if the
143<strong>second segment</strong> of the URI is empty. Another way to show your "Hello World" message would be this:</p>
144
145<code>example.com/index.php/<var>blog</var>/<samp>index</samp>/</code>
146
147<p><strong>The second segment of the URI determines which function in the controller gets called.</strong></p>
148
149<p>Let's try it. Add a new function to your controller:</p>
150
151
152<textarea class="textarea" style="width:100%" cols="50" rows="15">
Derek Allard4b6d4932008-12-07 17:04:56 +0000153&lt;?php
Greg Aker63277b82010-11-09 13:46:13 -0600154class Blog extends CI_Controller {
Derek Allard2067d1a2008-11-13 22:59:24 +0000155
156 function index()
157 {
158 echo 'Hello World!';
159 }
160
161 function comments()
162 {
163 echo 'Look at this!';
164 }
165}
Derek Allard4b6d4932008-12-07 17:04:56 +0000166?&gt;
Derek Allard2067d1a2008-11-13 22:59:24 +0000167</textarea>
168
169<p>Now load the following URL to see the <dfn>comment</dfn> function:</p>
170
171<code>example.com/index.php/<var>blog</var>/<samp>comments</samp>/</code>
172
173<p>You should see your new message.</p>
174
175<a name="passinguri"></a>
176<h2>Passing URI Segments to your Functions</h2>
177
178<p>If your URI contains more then two segments they will be passed to your function as parameters.</p>
179
180<p>For example, lets say you have a URI like this:</p>
181
182<code>example.com/index.php/<var>products</var>/<samp>shoes</samp>/<kbd>sandals</kbd>/<dfn>123</dfn></code>
183
184<p>Your function will be passed URI segments 3 and 4 ("sandals" and "123"):</p>
185
186<code>
187&lt;?php<br />
Greg Aker63277b82010-11-09 13:46:13 -0600188class Products extends CI_Controller {<br />
Derek Allard2067d1a2008-11-13 22:59:24 +0000189<br />
190&nbsp;&nbsp;&nbsp;&nbsp;function shoes($sandals, $id)<br />
191&nbsp;&nbsp;&nbsp;&nbsp;{<br />
192&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $sandals;<br />
193&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $id;<br />
194&nbsp;&nbsp;&nbsp;&nbsp;}<br />
195}<br />
196?&gt;
197</code>
198
199<p class="important"><strong>Important:</strong>&nbsp; If you are using the <a href="routing.html">URI Routing</a> feature, the segments
200passed to your function will be the re-routed ones.</p>
201
202
203<a name="default"></a>
204<h2>Defining a Default Controller</h2>
205
206<p>CodeIgniter can be told to load a default controller when a URI is not present,
207as will be the case when only your site root URL is requested. To specify a default controller, open
208your <dfn>application/config/routes.php</dfn> file and set this variable:</p>
209
210<code>$route['default_controller'] = '<var>Blog</var>';</code>
211
212<p>Where <var>Blog</var> is the name of the controller class you want used. If you now load your main index.php file without
213specifying any URI segments you'll see your Hello World message by default.</p>
214
215
216
217<a name="remapping"></a>
218<h2>Remapping Function Calls</h2>
219
220<p>As noted above, the second segment of the URI typically determines which function in the controller gets called.
221CodeIgniter permits you to override this behavior through the use of the <kbd>_remap()</kbd> function:</p>
222
223<code>function _remap()<br />
224{<br />
225&nbsp;&nbsp;&nbsp;&nbsp;// Some code here...<br />
226}</code>
227
228<p class="important"><strong>Important:</strong>&nbsp; If your controller contains a function named <kbd>_remap()</kbd>, it will <strong>always</strong>
229get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called,
230allowing you to define your own function routing rules.</p>
231
Pascal Kriete3431ae32010-11-09 15:19:50 -0500232<p>The overridden function call (typically the second segment of the URI) will be passed as a parameter to the <kbd>_remap()</kbd> function:</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000233
234<code>function _remap(<var>$method</var>)<br />
235{<br />
236&nbsp;&nbsp;&nbsp;&nbsp;if ($method == 'some_method')<br />
237&nbsp;&nbsp;&nbsp;&nbsp;{<br />
238&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->$method();<br />
239&nbsp;&nbsp;&nbsp;&nbsp;}<br />
240&nbsp;&nbsp;&nbsp;&nbsp;else<br />
241&nbsp;&nbsp;&nbsp;&nbsp;{<br />
242&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->default_method();<br />
243&nbsp;&nbsp;&nbsp;&nbsp;}<br />
244}</code>
245
Pascal Kriete3431ae32010-11-09 15:19:50 -0500246<p>An array of leftover segments are passed into <kbd>_remap()</kbd> as an optional second parameter.</p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000247
248
249
250
251<a name="output"></a>
252<h2>Processing Output</h2>
253
254<p>CodeIgniter has an output class that takes care of sending your final rendered data to the web browser automatically. More information on this can be found in the
255<a href="views.html">Views</a> and <a href="../libraries/output.html">Output class</a> pages. In some cases, however, you might want to
256post-process the finalized data in some way and send it to the browser yourself. CodeIgniter permits you to
257add a function named <dfn>_output()</dfn> to your controller that will receive the finalized output data.</p>
258
259<p><strong>Important:</strong>&nbsp; If your controller contains a function named <kbd>_output()</kbd>, it will <strong>always</strong>
260be called by the output class instead of echoing the finalized data directly. The first parameter of the function will contain the finalized output.</p>
261
262<p>Here is an example:</p>
263
264<code>
265function _output($output)<br />
266{<br />
267&nbsp;&nbsp;&nbsp;&nbsp;echo $output;<br />
268}</code>
269
270<p class="important">Please note that your <dfn>_output()</dfn> function will receive the data in its finalized state. Benchmark and memory usage data will be rendered,
271cache files written (if you have caching enabled), and headers will be sent (if you use that <a href="../libraries/output.html">feature</a>)
Derek Jonesd7633492010-09-28 13:14:57 -0500272before it is handed off to the _output() function.<br />
273<br />
274To have your controller's output cached properly, its <dfn>_output()</dfn> method can use:<br />
275
276<code>if ($this-&gt;output-&gt;cache_expiration &gt; 0)<br />
277{<br />
Derek Jones3351fbc2010-10-01 09:56:31 -0500278&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;output-&gt;_write_cache($output);<br />
Derek Jonesd7633492010-09-28 13:14:57 -0500279}</code>
280
281If you are using this feature the page execution timer and memory usage stats might not be perfectly accurate
Derek Allard2067d1a2008-11-13 22:59:24 +0000282since they will not take into acccount any further processing you do. For an alternate way to control output <em>before</em> any of the final processing is done, please see
283the available methods in the <a href="../libraries/output.html">Output Class</a>.</p>
284
285<a name="private"></a>
286<h2>Private Functions</h2>
287
288
289<p>In some cases you may want certain functions hidden from public access. To make a function private, simply add an
290underscore as the name prefix and it will not be served via a URL request. For example, if you were to have a function like this:</p>
291
292<code>
293function _utility()<br />
294{<br />
295&nbsp;&nbsp;// some code<br />
296}</code>
297
298<p>Trying to access it via the URL, like this, will not work:</p>
299
300<code>example.com/index.php/<var>blog</var>/<samp>_utility</samp>/</code>
301
302
303
304<a name="subfolders"></a>
305<h2>Organizing Your Controllers into Sub-folders</h2>
306
307<p>If you are building a large application you might find it convenient to organize your controllers into sub-folders. CodeIgniter permits you to do this.</p>
308
309<p>Simply create folders within your <dfn>application/controllers</dfn> directory and place your controller classes within them.</p>
310
311<p><strong>Note:</strong>&nbsp; When using this feature the first segment of your URI must specify the folder. For example, lets say you have a controller
312located here:</p>
313
314<code>application/controllers/<kbd>products</kbd>/shoes.php</code>
315
316<p>To call the above controller your URI will look something like this:</p>
317
318<code>example.com/index.php/products/shoes/show/123</code>
319
320<p>Each of your sub-folders may contain a default controller which will be
321called if the URL contains only the sub-folder. Simply name your default controller as specified in your
322<dfn>application/config/routes.php</dfn> file</p>
323
324
325<p>CodeIgniter also permits you to remap your URIs using its <a href="routing.html">URI Routing</a> feature.</p>
326
327
328<h2><a name="constructors"></a>Class Constructors</h2>
329
330
331<p>If you intend to use a constructor in any of your Controllers, you <strong>MUST</strong> place the following line of code in it:</p>
332
Pascal Krietea7056982010-11-10 16:12:55 -0500333<code>parent::__construct();</code>
Derek Allard2067d1a2008-11-13 22:59:24 +0000334
335<p>The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.</p>
336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337<code>
338&lt;?php<br />
Greg Aker63277b82010-11-09 13:46:13 -0600339class <kbd>Blog</kbd> extends CI_Controller {<br />
Derek Allard2067d1a2008-11-13 22:59:24 +0000340<br />
341&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function <kbd>__construct()</kbd><br />
342&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
Greg Aker63277b82010-11-09 13:46:13 -0600343&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<var>parent::CI_Controller();</var><br />
Pascal Krietea7056982010-11-10 16:12:55 -0500344&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Your own constructor code<br />
Derek Allard2067d1a2008-11-13 22:59:24 +0000345&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
346}<br />
347?&gt;</code>
348
349<p>Constructors are useful if you need to set some default values, or run a default process when your class is instantiated.
350Constructors can't return a value, but they can do some default work.</p>
351
352<a name="reserved"></a>
353<h2>Reserved Function Names</h2>
354
355<p>Since your controller classes will extend the main application controller you
356must be careful not to name your functions identically to the ones used by that class, otherwise your local functions
357will override them. See <a href="reserved_names.html">Reserved Names</a> for a full list.</p>
358
359<h2>That's it!</h2>
360
361<p>That, in a nutshell, is all there is to know about controllers.</p>
362
363
364
365</div>
366<!-- END CONTENT -->
367
368
369<div id="footer">
370<p>
371Previous Topic:&nbsp;&nbsp;<a href="urls.html">CodeIgniter URLs</a>
372&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
373<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
374<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
375Next Topic:&nbsp;&nbsp;<a href="reserved_names.html">Reserved Names</a></p>
Derek Jonesd6d70e32010-03-29 10:10:27 -0500376<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2010 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000377</div>
378
379</body>
adminb0dd10f2006-08-25 17:25:49 +0000380</html>