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