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 |
|
Derek Allard | 404e35d | 2007-08-07 01:00:45 +0000 | [diff] [blame] | 5 | <title>CodeIgniter User Guide : Controllers</title>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 14 |
|
| 15 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 16 | <meta http-equiv='expires' content='-1' />
|
| 17 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 18 | <meta name='robots' content='all' />
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame^] | 19 | <meta name='author' content='ExpressionEngine Dev Team' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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.jpg" width="153" height="44" 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 Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.6.0</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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">
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 43 | <a href="http://www.codeigniter.com/">CodeIgniter Home</a> ›
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | Controllers
|
| 46 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 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 <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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>
|
admin | 2c676ef | 2006-09-21 16:54:44 +0000 | [diff] [blame] | 67 | <li><a href="#passinguri">Passing URI Segments to Your Functions</a></li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 68 | <li><a href="#default">Defining a Default Controller</a></li>
|
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 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>
|
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 72 | <li><a href="#subfolders">Organizing Controllers into Sub-folders</a></li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>www.your-site.com/index.php/<var>blog</var>/</code>
|
| 86 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 87 | <p>In the above example, CodeIgniter would attempt to find a controller named <dfn>blog.php</dfn> and load it.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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: 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">
|
| 98 | <?php
|
| 99 | class Blog extends Controller {
|
| 100 |
|
| 101 | function index()
|
| 102 | {
|
| 103 | echo 'Hello World!';
|
| 104 | }
|
| 105 | }
|
| 106 | ?>
|
| 107 | </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>www.your-site.com/index.php/<var>blog</var>/</code>
|
| 116 |
|
| 117 | <p>If you did it right, you should see <samp>Hello World!</samp>.</p>
|
| 118 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 119 | <p>Note: Class names must start with an uppercase letter. In other words, this is valid:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 120 |
|
| 121 | <code><?php<br />
|
| 122 | class <var>Blog</var> extends Controller {<br />
|
| 123 | <br />
|
| 124 | }<br />
|
| 125 | ?></code>
|
| 126 |
|
| 127 | <p>This is <strong>not</strong> valid:</p>
|
| 128 |
|
| 129 | <code><?php<br />
|
| 130 | class <var>blog</var> extends Controller {<br />
|
| 131 | <br />
|
| 132 | }<br />
|
| 133 | ?></code>
|
| 134 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 135 | <p>Also, always make sure your controller <dfn>extends</dfn> the parent controller class so that it can inherit all its functions.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 136 |
|
| 137 |
|
| 138 |
|
| 139 | <a name="functions"></a>
|
| 140 | <h2>Functions</h2>
|
| 141 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 142 | <p>In the above example the function name is <dfn>index()</dfn>. The "index" function is always loaded by default if the
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>www.your-site.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">
|
| 153 | <?php
|
| 154 | class Blog extends Controller {
|
| 155 |
|
| 156 | function index()
|
| 157 | {
|
| 158 | echo 'Hello World!';
|
| 159 | }
|
| 160 |
|
| 161 | function comments()
|
| 162 | {
|
| 163 | echo 'Look at this!';
|
| 164 | }
|
| 165 | }
|
| 166 | ?>
|
| 167 | </textarea>
|
| 168 |
|
| 169 | <p>Now load the following URL to see the <dfn>comment</dfn> function:</p>
|
| 170 |
|
| 171 | <code>www.your-site.com/index.php/<var>blog</var>/<samp>comments</samp>/</code>
|
| 172 |
|
| 173 | <p>You should see your new message.</p>
|
| 174 |
|
admin | 2c676ef | 2006-09-21 16:54:44 +0000 | [diff] [blame] | 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 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 180 | <p>For example, lets say you have a URI like this:</p>
|
admin | 2c676ef | 2006-09-21 16:54:44 +0000 | [diff] [blame] | 181 |
|
| 182 | <code>www.your-site.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 | <?php<br />
|
| 188 | class Products extends Controller {<br />
|
| 189 | <br />
|
| 190 | function shoes($sandals, $id)<br />
|
| 191 | {<br />
|
| 192 | echo $sandals;<br />
|
| 193 | echo $id;<br />
|
| 194 | }<br />
|
| 195 | }<br />
|
| 196 | ?>
|
| 197 | </code>
|
| 198 |
|
| 199 | <p class="important"><strong>Important:</strong> If you are using the <a href="routing.html">URI Routing</a> feature, the segments
|
| 200 | passed to your function will be the re-routed ones.</p>
|
| 201 |
|
| 202 |
|
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 203 | <a name="default"></a>
|
| 204 | <h2>Defining a Default Controller</h2>
|
| 205 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 206 | <p>CodeIgniter can be told to load a default controller when a URI is not present,
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 207 | as will be the case when only your site root URL is requested. To specify a default controller, open
|
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 208 | your <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
|
| 213 | specifying any URI segments you'll see your Hello World message by default.</p>
|
| 214 |
|
| 215 |
|
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 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.
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 221 | CodeIgniter permits you to override this behavior through the use of the <kbd>_remap()</kbd> function:</p>
|
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 222 |
|
| 223 | <code>function _remap()<br />
|
| 224 | {<br />
|
| 225 | // Some code here...<br />
|
| 226 | }</code>
|
| 227 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 228 | <p class="important"><strong>Important:</strong> If your controller contains a function named <kbd>_remap()</kbd>, it will <strong>always</strong>
|
| 229 | get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called,
|
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 230 | allowing you to define your own function routing rules.</p>
|
| 231 |
|
admin | 2c676ef | 2006-09-21 16:54:44 +0000 | [diff] [blame] | 232 | <p>The overridden function call (typically the second segment of the URI) will be passed as a parameter the <kbd>_remap()</kbd> function:</p>
|
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 233 |
|
| 234 | <code>function _remap(<var>$method</var>)<br />
|
| 235 | {<br />
|
| 236 | if ($method == 'some_method')<br />
|
| 237 | {<br />
|
| 238 | $this->$method();<br />
|
| 239 | }<br />
|
| 240 | else<br />
|
| 241 | {<br />
|
| 242 | $this->default_method();<br />
|
| 243 | }<br />
|
| 244 | }</code>
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 249 |
|
| 250 | <a name="output"></a>
|
| 251 | <h2>Processing Output</h2>
|
| 252 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 253 | <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
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 254 | <a href="views.html">Views</a> and <a href="../libraries/output.html">Output class</a> pages. In some cases, however, you might want to
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 255 | post-process the finalized data in some way and send it to the browser yourself. CodeIgniter permits you to
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 256 | add a function named <dfn>_output()</dfn> to your controller that will receive the finalized output data.</p>
|
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 257 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 258 | <p><strong>Important:</strong> If your controller contains a function named <kbd>_output()</kbd>, it will <strong>always</strong>
|
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 259 | be called by the output class instead of echoing the finalized data directly. The first parameter of the function will contain the finalized output.</p>
|
| 260 |
|
| 261 | <p>Here is an example:</p>
|
| 262 |
|
| 263 | <code>
|
| 264 | function _output($output)<br />
|
| 265 | {<br />
|
| 266 | echo $output;<br />
|
| 267 | }</code>
|
| 268 |
|
admin | baa5818 | 2006-09-23 03:59:30 +0000 | [diff] [blame] | 269 | <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,
|
Derek Allard | 4b7d19d | 2007-07-11 19:44:50 +0000 | [diff] [blame] | 270 | cache files written (if you have caching enabled), and headers will be sent (if you use that <a href="../libraries/output.html">feature</a>)
|
admin | baa5818 | 2006-09-23 03:59:30 +0000 | [diff] [blame] | 271 | before it is handed off to the _output() function. If you are using this feature the page execution timer and memory usage stats might not be perfectly accurate
|
| 272 | since 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
|
Derek Allard | 4b7d19d | 2007-07-11 19:44:50 +0000 | [diff] [blame] | 273 | the available methods in the <a href="../libraries/output.html">Output Class</a>.</p>
|
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 274 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 275 | <a name="private"></a>
|
| 276 | <h2>Private Functions</h2>
|
| 277 |
|
admin | c1fa074 | 2006-09-21 23:50:23 +0000 | [diff] [blame] | 278 |
|
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 279 | <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] | 280 | 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>
|
| 281 |
|
| 282 | <code>
|
| 283 | function _utility()<br />
|
| 284 | {<br />
|
| 285 | // some code<br />
|
| 286 | }</code>
|
| 287 |
|
| 288 | <p>Trying to access it via the URL, like this, will not work:</p>
|
| 289 |
|
| 290 | <code>www.your-site.com/index.php/<var>blog</var>/<samp>_utility</samp>/</code>
|
| 291 |
|
| 292 |
|
| 293 |
|
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 294 | <a name="subfolders"></a>
|
| 295 | <h2>Organizing Your Controllers into Sub-folders</h2>
|
| 296 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 297 | <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>
|
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 298 |
|
| 299 | <p>Simply create folders within your <dfn>application/controllers</dfn> directory and place your controller classes within them.</p>
|
| 300 |
|
admin | 1082bdd | 2006-08-27 19:32:02 +0000 | [diff] [blame] | 301 | <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] | 302 | located here:</p>
|
| 303 |
|
| 304 | <code>application/controllers/<kbd>products</kbd>/shoes.php</code>
|
| 305 |
|
| 306 | <p>To call the above controller your URI will look something like this:</p>
|
| 307 |
|
| 308 | <code>www.your-site.com/index.php/products/shoes/123</code>
|
| 309 |
|
admin | e3817a3 | 2006-09-05 03:30:46 +0000 | [diff] [blame] | 310 | <p>Each of your sub-folders may contain a default controller which will be
|
| 311 | called if the URL contains only the sub-folder. Simply name your default controller as specified in your
|
| 312 | <dfn>application/config/routes.php</dfn> file</p>
|
| 313 |
|
| 314 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 315 | <p>CodeIgniter also permits you to remap your URIs using its <a href="routing.html">URI Routing</a> feature.</p>
|
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 316 |
|
| 317 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 318 | <h2><a name="constructors"></a>Class Constructors</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 319 |
|
| 320 |
|
| 321 | <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>
|
| 322 |
|
| 323 | <code>parent::Controller();</code>
|
| 324 |
|
| 325 | <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>
|
| 326 |
|
| 327 |
|
admin | 2c676ef | 2006-09-21 16:54:44 +0000 | [diff] [blame] | 328 | <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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 329 |
|
| 330 | <code>
|
| 331 | <?php<br />
|
| 332 | class <kbd>Blog</kbd> extends Controller {<br />
|
| 333 | <br />
|
| 334 | function <kbd>Blog()</kbd><br />
|
| 335 | {<br />
|
| 336 | <var>parent::Controller();</var><br />
|
| 337 | }<br />
|
| 338 | }<br />
|
| 339 | ?></code>
|
| 340 |
|
| 341 | <p>In PHP 5, constructors use the following syntax:</p>
|
| 342 |
|
| 343 | <code>
|
| 344 | <?php<br />
|
| 345 | class <kbd>Blog</kbd> extends Controller {<br />
|
| 346 | <br />
|
| 347 | function <kbd>__construct()</kbd><br />
|
| 348 | {<br />
|
| 349 | <var>parent::Controller();</var><br />
|
| 350 | }<br />
|
| 351 | }<br />
|
| 352 | ?></code>
|
| 353 |
|
| 354 | <p>Constructors are useful if you need to set some default values, or run a default process when your class is instantiated.
|
| 355 | Constructors can't return a value, but they can do some default work.</p>
|
| 356 |
|
| 357 | <a name="reserved"></a>
|
| 358 | <h2>Reserved Function Names</h2>
|
| 359 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 360 | <p>Since your controller classes will extend the main application controller you
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 361 | must be careful not to name your functions identically to the ones used by that class, otherwise your local functions
|
| 362 | will override them. The following
|
| 363 | is a list of reserved names. Do not name your controller functions any of these:</p>
|
| 364 |
|
| 365 | <ul>
|
| 366 | <li>Controller</li>
|
| 367 | <li>CI_Base</li>
|
admin | 81ccb43 | 2006-10-07 03:08:40 +0000 | [diff] [blame] | 368 | <li>_ci_initialize</li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 369 | <li>_ci_scaffolding</li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 370 | </ul>
|
| 371 |
|
| 372 | <p><br />If you are running PHP 4 there are some additional reserved names. These ONLY apply if you are running PHP 4.</p>
|
| 373 |
|
| 374 | <ul>
|
| 375 | <li>CI_Loader</li>
|
| 376 | <li>config</li>
|
| 377 | <li>database</li>
|
| 378 | <li>file</li>
|
| 379 | <li>helper</li>
|
| 380 | <li>helpers</li>
|
| 381 | <li>language</li>
|
| 382 | <li>library</li>
|
admin | fd2750b | 2006-10-06 17:29:12 +0000 | [diff] [blame] | 383 | <li>model</li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 384 | <li>plugin</li>
|
| 385 | <li>plugins</li>
|
| 386 | <li>scaffolding</li>
|
| 387 | <li>script</li>
|
| 388 | <li>view</li>
|
| 389 | <li>vars</li>
|
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 390 | <li>_ci_assign_to_models</li>
|
admin | fd2750b | 2006-10-06 17:29:12 +0000 | [diff] [blame] | 391 | <li>_ci_autoloader</li>
|
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 392 | <li>_ci_init_class</li>
|
| 393 | <li>_ci_init_scaffolding</li>
|
admin | 4003718 | 2006-10-11 19:16:58 +0000 | [diff] [blame] | 394 | <li>_ci_is_instance</li>
|
admin | 81ccb43 | 2006-10-07 03:08:40 +0000 | [diff] [blame] | 395 | <li>_ci_load</li>
|
| 396 | <li>_ci_load_class</li>
|
| 397 | <li>_ci_object_to_array</li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 398 | </ul>
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 | <h2>That's it!</h2>
|
| 406 |
|
| 407 | <p>That, in a nutshell, is all there is to know about controllers.</p>
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 | </div>
|
| 412 | <!-- END CONTENT -->
|
| 413 |
|
| 414 |
|
| 415 | <div id="footer">
|
| 416 | <p>
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 417 | Previous Topic: <a href="urls.html">CodeIgniter URLs</a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 418 | ·
|
| 419 | <a href="#top">Top of Page</a> ·
|
| 420 | <a href="../index.html">User Guide Home</a> ·
|
| 421 | Next Topic: <a href="views.html">Views</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 422 | </p>
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 423 | <p><a href="http://www.codeigniter.com">CodeIgniter</a> · Copyright © 2007 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 424 | </div>
|
| 425 |
|
| 426 | </body>
|
| 427 | </html> |