blob: 3ea0b61c4162fe447b72baadb20c91e85732ff5b [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>
36<td><h1>Code Igniter User Guide Version 1.4.0</h1></td>
37<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>
admin1cf89aa2006-09-03 18:24:39 +000072<li><a href="#remapping">Remapping Function Calls</a></li>
adminb0dd10f2006-08-25 17:25:49 +000073<li><a href="#private">Private Functions</a></li>
74<li><a href="#default">Defining a Default Controller</a></li>
adminb071bb52006-08-26 19:28:37 +000075<li><a href="#subfolders">Organizing Controllers into Sub-folders</a></li>
adminb0dd10f2006-08-25 17:25:49 +000076<li><a href="#constructors">Class Constructors</a></li>
77<li><a href="#reserved">Reserved Function Names</a></li>
78</ul>
79
80
81<a name="what"></a>
82<h2>What is a Controller?</h2>
83
84<p><dfn>A Controller is simply a class file that is named in a way that can be associated with a URI.</dfn></p>
85
86<p>Consider this URI:</p>
87
88<code>www.your-site.com/index.php/<var>blog</var>/</code>
89
90<p>In the above example, Code Igniter would attempt to find a controller named <dfn>blog.php</dfn> and load it.</p>
91
92<p><strong>When a controller's name matches the first segment of a URI, it will be loaded.</strong></p>
93
94<a name="hello"></a>
95<h2>Let's try it:&nbsp; Hello World!</h2>
96
97<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>
98
99
100<textarea class="textarea" style="width:100%" cols="50" rows="10">
101<?php
102class Blog extends Controller {
103
104 function index()
105 {
106 echo 'Hello World!';
107 }
108}
109?>
110</textarea>
111
112
113
114<p>Then save the file to your <dfn>application/controllers/</dfn> folder.</p>
115
116<p>Now visit the your site using a URL similar to this:</p>
117
118<code>www.your-site.com/index.php/<var>blog</var>/</code>
119
120<p>If you did it right, you should see <samp>Hello World!</samp>.</p>
121
122<p>Note: Class names must start with an uppercase letter. In other words, this is valid:
123
124<code>&lt;?php<br />
125class <var>Blog</var> extends Controller {<br />
126<br />
127}<br />
128?&gt;</code>
129
130<p>This is <strong>not</strong> valid:</p>
131
132<code>&lt;?php<br />
133class <var>blog</var> extends Controller {<br />
134<br />
135}<br />
136?&gt;</code>
137
138<p>Also, always make sure your controller <dfn>extends</dfn> the parent controller class so that it can inherit all its functions.</p>
139
140
141
142<a name="functions"></a>
143<h2>Functions</h2>
144
145<p>In the above example the function name is <dfn>index()</dfn>. The "index" function is always loaded by default if the
146<strong>second segment</strong> of the URI is empty. Another way to show your "Hello World" message would be this:</p>
147
148<code>www.your-site.com/index.php/<var>blog</var>/<samp>index</samp>/</code>
149
150<p><strong>The second segment of the URI determines which function in the controller gets called.</strong></p>
151
152<p>Let's try it. Add a new function to your controller:</p>
153
154
155<textarea class="textarea" style="width:100%" cols="50" rows="15">
156<?php
157class Blog extends Controller {
158
159 function index()
160 {
161 echo 'Hello World!';
162 }
163
164 function comments()
165 {
166 echo 'Look at this!';
167 }
168}
169?>
170</textarea>
171
172<p>Now load the following URL to see the <dfn>comment</dfn> function:</p>
173
174<code>www.your-site.com/index.php/<var>blog</var>/<samp>comments</samp>/</code>
175
176<p>You should see your new message.</p>
177
admin1cf89aa2006-09-03 18:24:39 +0000178
179<a name="remapping"></a>
180<h2>Remapping Function Calls</h2>
181
182<p>As noted above, the second segment of the URI typically determines which function in the controller gets called.
183Code Igniter permits you to override this behavior through the use of the <kbd>_remap()</kbd> function:</p>
184
185<code>function _remap()<br />
186{<br />
187&nbsp;&nbsp;&nbsp;&nbsp;// Some code here...<br />
188}</code>
189
190<p class="important"><strong>Important:</strong>&nbsp; If your controller contains a function named <kbd>_remap()</kbd>, it will <strong>always</strong>
191get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called,
192allowing you to define your own function routing rules.</p>
193
194<p>The overriden function call (typically the second segment of the URI) will be passed as a parameter the <kbd>_remap()</kbd> function:</p>
195
196<code>function _remap(<var>$method</var>)<br />
197{<br />
198&nbsp;&nbsp;&nbsp;&nbsp;if ($method == 'some_method')<br />
199&nbsp;&nbsp;&nbsp;&nbsp;{<br />
200&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->$method();<br />
201&nbsp;&nbsp;&nbsp;&nbsp;}<br />
202&nbsp;&nbsp;&nbsp;&nbsp;else<br />
203&nbsp;&nbsp;&nbsp;&nbsp;{<br />
204&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->default_method();<br />
205&nbsp;&nbsp;&nbsp;&nbsp;}<br />
206}</code>
207
208
209
210
adminb0dd10f2006-08-25 17:25:49 +0000211<a name="private"></a>
212<h2>Private Functions</h2>
213
admin1cf89aa2006-09-03 18:24:39 +0000214<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 +0000215underscore 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>
216
217<code>
218function _utility()<br />
219{<br />
220&nbsp;&nbsp;// some code<br />
221}</code>
222
223<p>Trying to access it via the URL, like this, will not work:</p>
224
225<code>www.your-site.com/index.php/<var>blog</var>/<samp>_utility</samp>/</code>
226
227
228
229<a name="default"></a>
230<h2>Defining a Default Controller</h2>
231
232<p>Code Igniter can be told to load a default controller when a URI is not present,
233as will be the case when only your site root URL is requested. To specify a default controller, open
234your <dfn>application/config/routes.php</dfn> file and set this variable:</p>
235
236<code>$route['default_controller'] = '<var>Blog</var>';</code>
237
238<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
239specifying any URI segments you'll see your Hello World message by default.</p>
240
241
adminb071bb52006-08-26 19:28:37 +0000242<a name="subfolders"></a>
243<h2>Organizing Your Controllers into Sub-folders</h2>
244
245<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>
246
247<p>Simply create folders within your <dfn>application/controllers</dfn> directory and place your controller classes within them.</p>
248
admin1082bdd2006-08-27 19:32:02 +0000249<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 +0000250located here:</p>
251
252<code>application/controllers/<kbd>products</kbd>/shoes.php</code>
253
254<p>To call the above controller your URI will look something like this:</p>
255
256<code>www.your-site.com/index.php/products/shoes/123</code>
257
258<p>Code Igniter also permits you to remap your URIs using its <a href="routing.html">URI Routing</a> feature.
259
260
adminb0dd10f2006-08-25 17:25:49 +0000261<a name="constructors"></a>
262<h2>Class Constructors</h2>
263
264
265<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>
266
267<code>parent::Controller();</code>
268
269<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>
270
271
272<p>If you are not familliar with constructors, in PHP 4, a <em>constructor</em> is simply a function that has the exact same name as the class:</p>
273
274<code>
275&lt;?php<br />
276class <kbd>Blog</kbd> extends Controller {<br />
277<br />
278&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function <kbd>Blog()</kbd><br />
279&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
280&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<var>parent::Controller();</var><br />
281&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
282}<br />
283?&gt;</code>
284
285<p>In PHP 5, constructors use the following syntax:</p>
286
287<code>
288&lt;?php<br />
289class <kbd>Blog</kbd> extends Controller {<br />
290<br />
291&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function <kbd>__construct()</kbd><br />
292&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
293&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<var>parent::Controller();</var><br />
294&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
295}<br />
296?&gt;</code>
297
298<p>Constructors are useful if you need to set some default values, or run a default process when your class is instantiated.
299Constructors can't return a value, but they can do some default work.</p>
300
301<a name="reserved"></a>
302<h2>Reserved Function Names</h2>
303
304<p>Since your controller classes will extend the main application controller you
305must be careful not to name your functions identically to the ones used by that class, otherwise your local functions
306will override them. The following
307is a list of reserved names. Do not name your controller functions any of these:</p>
308
309<ul>
310<li>Controller</li>
311<li>CI_Base</li>
312<li>_ci_autoload</li>
313<li>_ci_autoloader</li>
314<li>_ci_assign_core</li>
315<li>_ci_initialize</li>
316<li>_ci_init_database</li>
317<li>_ci_init_scaffolding</li>
318<li>_ci_is_loaded</li>
319<li>_ci_load</li>
320<li>_ci_scaffolding</li>
321<li>_ci_set_view_path</li>
322</ul>
323
324<p><br />If you are running PHP 4 there are some additional reserved names. These ONLY apply if you are running PHP 4.</p>
325
326<ul>
327<li>CI_Loader</li>
328<li>config</li>
329<li>database</li>
330<li>file</li>
331<li>helper</li>
332<li>helpers</li>
333<li>language</li>
334<li>library</li>
335<li>plugin</li>
336<li>plugins</li>
337<li>scaffolding</li>
338<li>script</li>
339<li>view</li>
340<li>vars</li>
341</ul>
342
343
344
345
346
347
348<h2>That's it!</h2>
349
350<p>That, in a nutshell, is all there is to know about controllers.</p>
351
352
353
354</div>
355<!-- END CONTENT -->
356
357
358<div id="footer">
359<p>
360Previous Topic:&nbsp;&nbsp;<a href="urls.html">Code Igniter URLs</a>
361&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
362<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
363<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
364Next Topic:&nbsp;&nbsp;<a href="views.html">Views</a>
365<p>
366
367<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>
368</div>
369
370</body>
371</html>