diff options
Diffstat (limited to 'user_guide/general/controllers.html')
-rw-r--r-- | user_guide/general/controllers.html | 317 |
1 files changed, 317 insertions, 0 deletions
diff --git a/user_guide/general/controllers.html b/user_guide/general/controllers.html new file mode 100644 index 000000000..648298eb0 --- /dev/null +++ b/user_guide/general/controllers.html @@ -0,0 +1,317 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+<head>
+
+<title>Code Igniter User Guide</title>
+
+<style type='text/css' media='all'>@import url('../userguide.css');</style>
+<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
+
+<script type="text/javascript" src="../scripts/nav.js"></script>
+<script type="text/javascript" src="../scripts/prototype.lite.js"></script>
+<script type="text/javascript" src="../scripts/moo.fx.js"></script>
+<script type="text/javascript">
+window.onload = function() {
+ myHeight = new fx.Height('nav', {duration: 400});
+ myHeight.hide();
+}
+</script>
+
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<meta http-equiv='expires' content='-1' />
+<meta http-equiv= 'pragma' content='no-cache' />
+<meta name='robots' content='all' />
+<meta name='author' content='Rick Ellis' />
+<meta name='description' content='Code Igniter User Guide' />
+
+</head>
+<body>
+
+<!-- START NAVIGATION -->
+<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
+<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>
+<div id="masthead">
+<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
+<tr>
+<td><h1>Code Igniter User Guide Version 1.4.0</h1></td>
+<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
+</tr>
+</table>
+</div>
+<!-- END NAVIGATION -->
+
+
+<!-- START BREADCRUMB -->
+<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
+<tr>
+<td id="breadcrumb">
+<a href="http://www.codeigniter.com/">Code Igniter Home</a> ›
+<a href="../index.html">User Guide Home</a> ›
+Controllers
+</td>
+<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>
+</tr>
+</table>
+<!-- END BREADCRUMB -->
+
+<br clear="all" />
+
+
+<!-- START CONTENT -->
+<div id="content">
+
+<h1>Controllers</h1>
+
+<p>Controllers are the heart of your application, as they determine how HTTP requests should be handled.</p>
+
+
+<ul>
+<li><a href="#what">What is a Controller?</a></li>
+<li><a href="#hello">Hello World</a></li>
+<li><a href="#functions">Functions</a></li>
+<li><a href="#private">Private Functions</a></li>
+<li><a href="#default">Defining a Default Controller</a></li>
+<li><a href="#constructors">Class Constructors</a></li>
+<li><a href="#reserved">Reserved Function Names</a></li>
+</ul>
+
+
+<a name="what"></a>
+<h2>What is a Controller?</h2>
+
+<p><dfn>A Controller is simply a class file that is named in a way that can be associated with a URI.</dfn></p>
+
+<p>Consider this URI:</p>
+
+<code>www.your-site.com/index.php/<var>blog</var>/</code>
+
+<p>In the above example, Code Igniter would attempt to find a controller named <dfn>blog.php</dfn> and load it.</p>
+
+<p><strong>When a controller's name matches the first segment of a URI, it will be loaded.</strong></p>
+
+<a name="hello"></a>
+<h2>Let's try it: Hello World!</h2>
+
+<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>
+
+
+<textarea class="textarea" style="width:100%" cols="50" rows="10">
+<?php
+class Blog extends Controller {
+
+ function index()
+ {
+ echo 'Hello World!';
+ }
+}
+?>
+</textarea>
+
+
+
+<p>Then save the file to your <dfn>application/controllers/</dfn> folder.</p>
+
+<p>Now visit the your site using a URL similar to this:</p>
+
+<code>www.your-site.com/index.php/<var>blog</var>/</code>
+
+<p>If you did it right, you should see <samp>Hello World!</samp>.</p>
+
+<p>Note: Class names must start with an uppercase letter. In other words, this is valid:
+
+<code><?php<br />
+class <var>Blog</var> extends Controller {<br />
+<br />
+}<br />
+?></code>
+
+<p>This is <strong>not</strong> valid:</p>
+
+<code><?php<br />
+class <var>blog</var> extends Controller {<br />
+<br />
+}<br />
+?></code>
+
+<p>Also, always make sure your controller <dfn>extends</dfn> the parent controller class so that it can inherit all its functions.</p>
+
+
+
+<a name="functions"></a>
+<h2>Functions</h2>
+
+<p>In the above example the function name is <dfn>index()</dfn>. The "index" function is always loaded by default if the
+<strong>second segment</strong> of the URI is empty. Another way to show your "Hello World" message would be this:</p>
+
+<code>www.your-site.com/index.php/<var>blog</var>/<samp>index</samp>/</code>
+
+<p><strong>The second segment of the URI determines which function in the controller gets called.</strong></p>
+
+<p>Let's try it. Add a new function to your controller:</p>
+
+
+<textarea class="textarea" style="width:100%" cols="50" rows="15">
+<?php
+class Blog extends Controller {
+
+ function index()
+ {
+ echo 'Hello World!';
+ }
+
+ function comments()
+ {
+ echo 'Look at this!';
+ }
+}
+?>
+</textarea>
+
+<p>Now load the following URL to see the <dfn>comment</dfn> function:</p>
+
+<code>www.your-site.com/index.php/<var>blog</var>/<samp>comments</samp>/</code>
+
+<p>You should see your new message.</p>
+
+<a name="private"></a>
+<h2>Private Functions</h2>
+
+<p>In some cases you may not want certain functions accessible publicly. To make a function private, simply add an
+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>
+
+<code>
+function _utility()<br />
+{<br />
+ // some code<br />
+}</code>
+
+<p>Trying to access it via the URL, like this, will not work:</p>
+
+<code>www.your-site.com/index.php/<var>blog</var>/<samp>_utility</samp>/</code>
+
+
+
+<a name="default"></a>
+<h2>Defining a Default Controller</h2>
+
+<p>Code Igniter can be told to load a default controller when a URI is not present,
+as will be the case when only your site root URL is requested. To specify a default controller, open
+your <dfn>application/config/routes.php</dfn> file and set this variable:</p>
+
+<code>$route['default_controller'] = '<var>Blog</var>';</code>
+
+<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
+specifying any URI segments you'll see your Hello World message by default.</p>
+
+
+<a name="constructors"></a>
+<h2>Class Constructors</h2>
+
+
+<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>
+
+<code>parent::Controller();</code>
+
+<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>
+
+
+<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>
+
+<code>
+<?php<br />
+class <kbd>Blog</kbd> extends Controller {<br />
+<br />
+ function <kbd>Blog()</kbd><br />
+ {<br />
+ <var>parent::Controller();</var><br />
+ }<br />
+}<br />
+?></code>
+
+<p>In PHP 5, constructors use the following syntax:</p>
+
+<code>
+<?php<br />
+class <kbd>Blog</kbd> extends Controller {<br />
+<br />
+ function <kbd>__construct()</kbd><br />
+ {<br />
+ <var>parent::Controller();</var><br />
+ }<br />
+}<br />
+?></code>
+
+<p>Constructors are useful if you need to set some default values, or run a default process when your class is instantiated.
+Constructors can't return a value, but they can do some default work.</p>
+
+<a name="reserved"></a>
+<h2>Reserved Function Names</h2>
+
+<p>Since your controller classes will extend the main application controller you
+must be careful not to name your functions identically to the ones used by that class, otherwise your local functions
+will override them. The following
+is a list of reserved names. Do not name your controller functions any of these:</p>
+
+<ul>
+<li>Controller</li>
+<li>CI_Base</li>
+<li>_ci_autoload</li>
+<li>_ci_autoloader</li>
+<li>_ci_assign_core</li>
+<li>_ci_initialize</li>
+<li>_ci_init_database</li>
+<li>_ci_init_scaffolding</li>
+<li>_ci_is_loaded</li>
+<li>_ci_load</li>
+<li>_ci_scaffolding</li>
+<li>_ci_set_view_path</li>
+</ul>
+
+<p><br />If you are running PHP 4 there are some additional reserved names. These ONLY apply if you are running PHP 4.</p>
+
+<ul>
+<li>CI_Loader</li>
+<li>config</li>
+<li>database</li>
+<li>file</li>
+<li>helper</li>
+<li>helpers</li>
+<li>language</li>
+<li>library</li>
+<li>plugin</li>
+<li>plugins</li>
+<li>scaffolding</li>
+<li>script</li>
+<li>view</li>
+<li>vars</li>
+</ul>
+
+
+
+
+
+
+<h2>That's it!</h2>
+
+<p>That, in a nutshell, is all there is to know about controllers.</p>
+
+
+
+</div>
+<!-- END CONTENT -->
+
+
+<div id="footer">
+<p>
+Previous Topic: <a href="urls.html">Code Igniter URLs</a>
+ ·
+<a href="#top">Top of Page</a> ·
+<a href="../index.html">User Guide Home</a> ·
+Next Topic: <a href="views.html">Views</a>
+<p>
+
+<p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
+</div>
+
+</body>
+</html>
\ No newline at end of file |