blob: 5c51f8ff0759d6ed4025c7cb6dc858f914149ba6 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
5<title>Code Igniter User Guide</title>
6
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
10<script type="text/javascript" src="../scripts/nav.js"></script>
11<script type="text/javascript" src="../scripts/prototype.lite.js"></script>
12<script type="text/javascript" src="../scripts/moo.fx.js"></script>
13<script type="text/javascript">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
25<meta name='description' content='Code Igniter User Guide' />
26
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
admin9fc347d2006-09-21 00:00:28 +000036<td><h1>Code Igniter User Guide Version 1.4.1</h1></td>
adminb0dd10f2006-08-25 17:25:49 +000037<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Controllers
51</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
53</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63<h1>Controllers</h1>
64
65<p>Controllers are the heart of your application, as they determine how HTTP requests should be handled.</p>
66
67
68<ul>
69<li><a href="#what">What is a Controller?</a></li>
70<li><a href="#hello">Hello World</a></li>
71<li><a href="#functions">Functions</a></li>
admin2c676ef2006-09-21 16:54:44 +000072<li><a href="#passinguri">Passing URI Segments to Your Functions</a></li>
adminb0dd10f2006-08-25 17:25:49 +000073<li><a href="#default">Defining a Default Controller</a></li>
adminc1fa0742006-09-21 23:50:23 +000074<li><a href="#remapping">Remapping Function Calls</a></li>
75<li><a href="#output">Controlling Output Data</a></li>
76<li><a href="#private">Private Functions</a></li>
adminb071bb52006-08-26 19:28:37 +000077<li><a href="#subfolders">Organizing Controllers into Sub-folders</a></li>
adminb0dd10f2006-08-25 17:25:49 +000078<li><a href="#constructors">Class Constructors</a></li>
79<li><a href="#reserved">Reserved Function Names</a></li>
80</ul>
81
82
83<a name="what"></a>
84<h2>What is a Controller?</h2>
85
86<p><dfn>A Controller is simply a class file that is named in a way that can be associated with a URI.</dfn></p>
87
88<p>Consider this URI:</p>
89
90<code>www.your-site.com/index.php/<var>blog</var>/</code>
91
92<p>In the above example, Code Igniter would attempt to find a controller named <dfn>blog.php</dfn> and load it.</p>
93
94<p><strong>When a controller's name matches the first segment of a URI, it will be loaded.</strong></p>
95
96<a name="hello"></a>
97<h2>Let's try it:&nbsp; Hello World!</h2>
98
99<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>
100
101
102<textarea class="textarea" style="width:100%" cols="50" rows="10">
103<?php
104class Blog extends Controller {
105
106 function index()
107 {
108 echo 'Hello World!';
109 }
110}
111?>
112</textarea>
113
114
115
116<p>Then save the file to your <dfn>application/controllers/</dfn> folder.</p>
117
118<p>Now visit the your site using a URL similar to this:</p>
119
120<code>www.your-site.com/index.php/<var>blog</var>/</code>
121
122<p>If you did it right, you should see <samp>Hello World!</samp>.</p>
123
124<p>Note: Class names must start with an uppercase letter. In other words, this is valid:
125
126<code>&lt;?php<br />
127class <var>Blog</var> extends Controller {<br />
128<br />
129}<br />
130?&gt;</code>
131
132<p>This is <strong>not</strong> valid:</p>
133
134<code>&lt;?php<br />
135class <var>blog</var> extends Controller {<br />
136<br />
137}<br />
138?&gt;</code>
139
140<p>Also, always make sure your controller <dfn>extends</dfn> the parent controller class so that it can inherit all its functions.</p>
141
142
143
144<a name="functions"></a>
145<h2>Functions</h2>
146
147<p>In the above example the function name is <dfn>index()</dfn>. The "index" function is always loaded by default if the
148<strong>second segment</strong> of the URI is empty. Another way to show your "Hello World" message would be this:</p>
149
150<code>www.your-site.com/index.php/<var>blog</var>/<samp>index</samp>/</code>
151
152<p><strong>The second segment of the URI determines which function in the controller gets called.</strong></p>
153
154<p>Let's try it. Add a new function to your controller:</p>
155
156
157<textarea class="textarea" style="width:100%" cols="50" rows="15">
158<?php
159class Blog extends Controller {
160
161 function index()
162 {
163 echo 'Hello World!';
164 }
165
166 function comments()
167 {
168 echo 'Look at this!';
169 }
170}
171?>
172</textarea>
173
174<p>Now load the following URL to see the <dfn>comment</dfn> function:</p>
175
176<code>www.your-site.com/index.php/<var>blog</var>/<samp>comments</samp>/</code>
177
178<p>You should see your new message.</p>
179
admin2c676ef2006-09-21 16:54:44 +0000180<a name="passinguri"></a>
181<h2>Passing URI Segments to your Functions</h2>
182
183<p>If your URI contains more then two segments they will be passed to your function as parameters.</p>
184
185<p>For example, lets say you have a URI like this:
186
187<code>www.your-site.com/index.php/<var>products</var>/<samp>shoes</samp>/<kbd>sandals</kbd>/<dfn>123</dfn></code>
188
189<p>Your function will be passed URI segments 3 and 4 ("sandals" and "123"):</p>
190
191<code>
192&lt;?php<br />
193class Products extends Controller {<br />
194<br />
195&nbsp;&nbsp;&nbsp;&nbsp;function shoes($sandals, $id)<br />
196&nbsp;&nbsp;&nbsp;&nbsp;{<br />
197&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $sandals;<br />
198&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $id;<br />
199&nbsp;&nbsp;&nbsp;&nbsp;}<br />
200}<br />
201?&gt;
202</code>
203
204<p class="important"><strong>Important:</strong>&nbsp; If you are using the <a href="routing.html">URI Routing</a> feature, the segments
205passed to your function will be the re-routed ones.</p>
206
207
adminc1fa0742006-09-21 23:50:23 +0000208<a name="default"></a>
209<h2>Defining a Default Controller</h2>
210
211<p>Code Igniter can be told to load a default controller when a URI is not present,
212as will be the case when only your site root URL is requested. To specify a default controller, open
213your <dfn>application/config/routes.php</dfn> file and set this variable:</p>
214
215<code>$route['default_controller'] = '<var>Blog</var>';</code>
216
217<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
218specifying any URI segments you'll see your Hello World message by default.</p>
219
220
admin1cf89aa2006-09-03 18:24:39 +0000221
222<a name="remapping"></a>
223<h2>Remapping Function Calls</h2>
224
225<p>As noted above, the second segment of the URI typically determines which function in the controller gets called.
226Code Igniter permits you to override this behavior through the use of the <kbd>_remap()</kbd> function:</p>
227
228<code>function _remap()<br />
229{<br />
230&nbsp;&nbsp;&nbsp;&nbsp;// Some code here...<br />
231}</code>
232
233<p class="important"><strong>Important:</strong>&nbsp; If your controller contains a function named <kbd>_remap()</kbd>, it will <strong>always</strong>
234get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called,
235allowing you to define your own function routing rules.</p>
236
admin2c676ef2006-09-21 16:54:44 +0000237<p>The overridden function call (typically the second segment of the URI) will be passed as a parameter the <kbd>_remap()</kbd> function:</p>
admin1cf89aa2006-09-03 18:24:39 +0000238
239<code>function _remap(<var>$method</var>)<br />
240{<br />
241&nbsp;&nbsp;&nbsp;&nbsp;if ($method == 'some_method')<br />
242&nbsp;&nbsp;&nbsp;&nbsp;{<br />
243&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->$method();<br />
244&nbsp;&nbsp;&nbsp;&nbsp;}<br />
245&nbsp;&nbsp;&nbsp;&nbsp;else<br />
246&nbsp;&nbsp;&nbsp;&nbsp;{<br />
247&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->default_method();<br />
248&nbsp;&nbsp;&nbsp;&nbsp;}<br />
249}</code>
250
251
252
253
adminc1fa0742006-09-21 23:50:23 +0000254
255<a name="output"></a>
256<h2>Processing Output</h2>
257
258<p>Code Igniter 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
259<a href="views.html">Views</a> and <a href="../libraries/output.html">Output class</a> pages. In some cases, however, you might want to control
260how the output gets sent to the browser, or you might want to post process the finalized data in some way. Code Igniter permits you to
261add a function named <dfn>_output()</dfn> to your controller that will receive the finalized output data.
262
263<p class="important"><strong>Important:</strong>&nbsp; If your controller contains a function named <kbd>_output()</kbd>, it will <strong>always</strong>
264be called by the output class instead of echoing the finalized data directly. The first parameter of the function will contain the finalized output.</p>
265
266<p>Here is an example:</p>
267
268<code>
269function _output($output)<br />
270{<br />
271&nbsp;&nbsp;&nbsp;&nbsp;echo $output;<br />
272}</code>
273
274<p>Please note that your <dfn>_output()</dfn> function will receive the data in its finalized form - including rendered benchmark and memory usage data,
275so if you are using this feature the page execution timer might not be perfectly accurate.</p>
276
adminb0dd10f2006-08-25 17:25:49 +0000277<a name="private"></a>
278<h2>Private Functions</h2>
279
adminc1fa0742006-09-21 23:50:23 +0000280
admin1cf89aa2006-09-03 18:24:39 +0000281<p>In some cases you may want certain functions hidden from public access. To make a function private, simply add an
adminb0dd10f2006-08-25 17:25:49 +0000282underscore 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>
283
284<code>
285function _utility()<br />
286{<br />
287&nbsp;&nbsp;// some code<br />
288}</code>
289
290<p>Trying to access it via the URL, like this, will not work:</p>
291
292<code>www.your-site.com/index.php/<var>blog</var>/<samp>_utility</samp>/</code>
293
294
295
adminb071bb52006-08-26 19:28:37 +0000296<a name="subfolders"></a>
297<h2>Organizing Your Controllers into Sub-folders</h2>
298
299<p>If you are building a large application you might find it convenient to organize your controllers into sub-folders. Code Igniter permits you to do this.</p>
300
301<p>Simply create folders within your <dfn>application/controllers</dfn> directory and place your controller classes within them.</p>
302
admin1082bdd2006-08-27 19:32:02 +0000303<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
adminb071bb52006-08-26 19:28:37 +0000304located here:</p>
305
306<code>application/controllers/<kbd>products</kbd>/shoes.php</code>
307
308<p>To call the above controller your URI will look something like this:</p>
309
310<code>www.your-site.com/index.php/products/shoes/123</code>
311
admine3817a32006-09-05 03:30:46 +0000312<p>Each of your sub-folders may contain a default controller which will be
313called if the URL contains only the sub-folder. Simply name your default controller as specified in your
314<dfn>application/config/routes.php</dfn> file</p>
315
316
adminb071bb52006-08-26 19:28:37 +0000317<p>Code Igniter also permits you to remap your URIs using its <a href="routing.html">URI Routing</a> feature.
318
319
admine3817a32006-09-05 03:30:46 +0000320
adminb0dd10f2006-08-25 17:25:49 +0000321<a name="constructors"></a>
322<h2>Class Constructors</h2>
323
324
325<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>
326
327<code>parent::Controller();</code>
328
329<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>
330
331
admin2c676ef2006-09-21 16:54:44 +0000332<p>If you are not familiar with constructors, in PHP 4, a <em>constructor</em> is simply a function that has the exact same name as the class:</p>
adminb0dd10f2006-08-25 17:25:49 +0000333
334<code>
335&lt;?php<br />
336class <kbd>Blog</kbd> extends Controller {<br />
337<br />
338&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function <kbd>Blog()</kbd><br />
339&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
340&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<var>parent::Controller();</var><br />
341&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
342}<br />
343?&gt;</code>
344
345<p>In PHP 5, constructors use the following syntax:</p>
346
347<code>
348&lt;?php<br />
349class <kbd>Blog</kbd> extends Controller {<br />
350<br />
351&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function <kbd>__construct()</kbd><br />
352&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
353&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<var>parent::Controller();</var><br />
354&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
355}<br />
356?&gt;</code>
357
358<p>Constructors are useful if you need to set some default values, or run a default process when your class is instantiated.
359Constructors can't return a value, but they can do some default work.</p>
360
361<a name="reserved"></a>
362<h2>Reserved Function Names</h2>
363
364<p>Since your controller classes will extend the main application controller you
365must be careful not to name your functions identically to the ones used by that class, otherwise your local functions
366will override them. The following
367is a list of reserved names. Do not name your controller functions any of these:</p>
368
369<ul>
370<li>Controller</li>
371<li>CI_Base</li>
372<li>_ci_autoload</li>
373<li>_ci_autoloader</li>
374<li>_ci_assign_core</li>
375<li>_ci_initialize</li>
376<li>_ci_init_database</li>
377<li>_ci_init_scaffolding</li>
378<li>_ci_is_loaded</li>
379<li>_ci_load</li>
380<li>_ci_scaffolding</li>
381<li>_ci_set_view_path</li>
382</ul>
383
384<p><br />If you are running PHP 4 there are some additional reserved names. These ONLY apply if you are running PHP 4.</p>
385
386<ul>
387<li>CI_Loader</li>
388<li>config</li>
389<li>database</li>
390<li>file</li>
391<li>helper</li>
392<li>helpers</li>
393<li>language</li>
394<li>library</li>
395<li>plugin</li>
396<li>plugins</li>
397<li>scaffolding</li>
398<li>script</li>
399<li>view</li>
400<li>vars</li>
401</ul>
402
403
404
405
406
407
408<h2>That's it!</h2>
409
410<p>That, in a nutshell, is all there is to know about controllers.</p>
411
412
413
414</div>
415<!-- END CONTENT -->
416
417
418<div id="footer">
419<p>
420Previous Topic:&nbsp;&nbsp;<a href="urls.html">Code Igniter URLs</a>
421&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
422<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
423<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
424Next Topic:&nbsp;&nbsp;<a href="views.html">Views</a>
425<p>
426
427<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
428</div>
429
430</body>
431</html>