admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!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">
|
| 14 | window.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> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | Controllers
|
| 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 <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>
|
| 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>
|
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 72 | <li><a href="#remapping">Remapping Function Calls</a></li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 73 | <li><a href="#private">Private Functions</a></li>
|
| 74 | <li><a href="#default">Defining a Default Controller</a></li>
|
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 75 | <li><a href="#subfolders">Organizing Controllers into Sub-folders</a></li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 76 | <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: 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
|
| 102 | class 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><?php<br />
|
| 125 | class <var>Blog</var> extends Controller {<br />
|
| 126 | <br />
|
| 127 | }<br />
|
| 128 | ?></code>
|
| 129 |
|
| 130 | <p>This is <strong>not</strong> valid:</p>
|
| 131 |
|
| 132 | <code><?php<br />
|
| 133 | class <var>blog</var> extends Controller {<br />
|
| 134 | <br />
|
| 135 | }<br />
|
| 136 | ?></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
|
| 157 | class 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 |
|
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 178 |
|
| 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.
|
| 183 | Code 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 | // Some code here...<br />
|
| 188 | }</code>
|
| 189 |
|
| 190 | <p class="important"><strong>Important:</strong> If your controller contains a function named <kbd>_remap()</kbd>, it will <strong>always</strong>
|
| 191 | get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called,
|
| 192 | allowing 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 | if ($method == 'some_method')<br />
|
| 199 | {<br />
|
| 200 | $this->$method();<br />
|
| 201 | }<br />
|
| 202 | else<br />
|
| 203 | {<br />
|
| 204 | $this->default_method();<br />
|
| 205 | }<br />
|
| 206 | }</code>
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 211 | <a name="private"></a>
|
| 212 | <h2>Private Functions</h2>
|
| 213 |
|
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 214 | <p>In some cases you may want certain functions hidden from public access. To make a function private, simply add an
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 215 | underscore 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>
|
| 218 | function _utility()<br />
|
| 219 | {<br />
|
| 220 | // 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,
|
| 233 | as will be the case when only your site root URL is requested. To specify a default controller, open
|
| 234 | your <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
|
| 239 | specifying any URI segments you'll see your Hello World message by default.</p>
|
| 240 |
|
| 241 |
|
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 242 | <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 |
|
admin | 1082bdd | 2006-08-27 19:32:02 +0000 | [diff] [blame] | 249 | <p><strong>Note:</strong> When using this feature the first segment of your URI must specify the folder. For example, lets say you have a controller
|
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 250 | located 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 |
|
admin | e3817a3 | 2006-09-05 03:30:46 +0000 | [diff] [blame] | 258 | <p>Each of your sub-folders may contain a default controller which will be
|
| 259 | called if the URL contains only the sub-folder. Simply name your default controller as specified in your
|
| 260 | <dfn>application/config/routes.php</dfn> file</p>
|
| 261 |
|
| 262 |
|
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 263 | <p>Code Igniter also permits you to remap your URIs using its <a href="routing.html">URI Routing</a> feature.
|
| 264 |
|
| 265 |
|
admin | e3817a3 | 2006-09-05 03:30:46 +0000 | [diff] [blame] | 266 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 267 | <a name="constructors"></a>
|
| 268 | <h2>Class Constructors</h2>
|
| 269 |
|
| 270 |
|
| 271 | <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>
|
| 272 |
|
| 273 | <code>parent::Controller();</code>
|
| 274 |
|
| 275 | <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>
|
| 276 |
|
| 277 |
|
| 278 | <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>
|
| 279 |
|
| 280 | <code>
|
| 281 | <?php<br />
|
| 282 | class <kbd>Blog</kbd> extends Controller {<br />
|
| 283 | <br />
|
| 284 | function <kbd>Blog()</kbd><br />
|
| 285 | {<br />
|
| 286 | <var>parent::Controller();</var><br />
|
| 287 | }<br />
|
| 288 | }<br />
|
| 289 | ?></code>
|
| 290 |
|
| 291 | <p>In PHP 5, constructors use the following syntax:</p>
|
| 292 |
|
| 293 | <code>
|
| 294 | <?php<br />
|
| 295 | class <kbd>Blog</kbd> extends Controller {<br />
|
| 296 | <br />
|
| 297 | function <kbd>__construct()</kbd><br />
|
| 298 | {<br />
|
| 299 | <var>parent::Controller();</var><br />
|
| 300 | }<br />
|
| 301 | }<br />
|
| 302 | ?></code>
|
| 303 |
|
| 304 | <p>Constructors are useful if you need to set some default values, or run a default process when your class is instantiated.
|
| 305 | Constructors can't return a value, but they can do some default work.</p>
|
| 306 |
|
| 307 | <a name="reserved"></a>
|
| 308 | <h2>Reserved Function Names</h2>
|
| 309 |
|
| 310 | <p>Since your controller classes will extend the main application controller you
|
| 311 | must be careful not to name your functions identically to the ones used by that class, otherwise your local functions
|
| 312 | will override them. The following
|
| 313 | is a list of reserved names. Do not name your controller functions any of these:</p>
|
| 314 |
|
| 315 | <ul>
|
| 316 | <li>Controller</li>
|
| 317 | <li>CI_Base</li>
|
| 318 | <li>_ci_autoload</li>
|
| 319 | <li>_ci_autoloader</li>
|
| 320 | <li>_ci_assign_core</li>
|
| 321 | <li>_ci_initialize</li>
|
| 322 | <li>_ci_init_database</li>
|
| 323 | <li>_ci_init_scaffolding</li>
|
| 324 | <li>_ci_is_loaded</li>
|
| 325 | <li>_ci_load</li>
|
| 326 | <li>_ci_scaffolding</li>
|
| 327 | <li>_ci_set_view_path</li>
|
| 328 | </ul>
|
| 329 |
|
| 330 | <p><br />If you are running PHP 4 there are some additional reserved names. These ONLY apply if you are running PHP 4.</p>
|
| 331 |
|
| 332 | <ul>
|
| 333 | <li>CI_Loader</li>
|
| 334 | <li>config</li>
|
| 335 | <li>database</li>
|
| 336 | <li>file</li>
|
| 337 | <li>helper</li>
|
| 338 | <li>helpers</li>
|
| 339 | <li>language</li>
|
| 340 | <li>library</li>
|
| 341 | <li>plugin</li>
|
| 342 | <li>plugins</li>
|
| 343 | <li>scaffolding</li>
|
| 344 | <li>script</li>
|
| 345 | <li>view</li>
|
| 346 | <li>vars</li>
|
| 347 | </ul>
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 | <h2>That's it!</h2>
|
| 355 |
|
| 356 | <p>That, in a nutshell, is all there is to know about controllers.</p>
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 | </div>
|
| 361 | <!-- END CONTENT -->
|
| 362 |
|
| 363 |
|
| 364 | <div id="footer">
|
| 365 | <p>
|
| 366 | Previous Topic: <a href="urls.html">Code Igniter URLs</a>
|
| 367 | ·
|
| 368 | <a href="#top">Top of Page</a> ·
|
| 369 | <a href="../index.html">User Guide Home</a> ·
|
| 370 | Next Topic: <a href="views.html">Views</a>
|
| 371 | <p>
|
| 372 |
|
| 373 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 374 | </div>
|
| 375 |
|
| 376 | </body>
|
| 377 | </html> |