summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/general/controllers.rst
diff options
context:
space:
mode:
authorDerek Jones <derek.jones@ellislab.com>2011-10-06 00:30:50 +0200
committerDerek Jones <derek.jones@ellislab.com>2011-10-06 00:30:50 +0200
commite69b45663f06e84b3257fcd56616e299708599d1 (patch)
treeebb6082e79724f6c596648dca9e38eb2e023810d /user_guide_src/source/general/controllers.rst
parent9713cce9876fce5cb38c3ee24193ae3455c81755 (diff)
fixed code spacing in Controllers general docs
Diffstat (limited to 'user_guide_src/source/general/controllers.rst')
-rw-r--r--user_guide_src/source/general/controllers.rst141
1 files changed, 109 insertions, 32 deletions
diff --git a/user_guide_src/source/general/controllers.rst b/user_guide_src/source/general/controllers.rst
index 8f02df21f..4d6e8366c 100644
--- a/user_guide_src/source/general/controllers.rst
+++ b/user_guide_src/source/general/controllers.rst
@@ -38,10 +38,18 @@ Let's try it: Hello World!
Let's create a simple controller so you can see it in action. Using your
text editor, create a file called blog.php, and put the following code
-in it:
+in it::
+
+ <?php
+ class Blog extends CI_Controller {
+
+ public function index()
+ {
+ echo 'Hello World!';
+ }
+ }
+ ?>
-<?php class Blog extends CI_Controller { public function index() { echo
-'Hello World!'; } } ?>
Then save the file to your application/controllers/ folder.
Now visit the your site using a URL similar to this::
@@ -53,11 +61,20 @@ If you did it right, you should see Hello World!.
Note: Class names must start with an uppercase letter. In other words,
this is valid::
- <?php class Blog extends CI_Controller { } ?>
+ <?php
+ class Blog extends CI_Controller {
+
+ }
+ ?>
+
This is **not** valid::
- <?php class blog extends CI_Controller { } ?>
+ <?php
+ class blog extends CI_Controller {
+
+ }
+ ?>
Also, always make sure your controller extends the parent controller
class so that it can inherit all its functions.
@@ -74,11 +91,23 @@ empty. Another way to show your "Hello World" message would be this::
**The second segment of the URI determines which function in the
controller gets called.**
-Let's try it. Add a new function to your controller:
+Let's try it. Add a new function to your controller::
+
+ <?php
+ class Blog extends CI_Controller {
+
+ public function index()
+ {
+ echo 'Hello World!';
+ }
+
+ public function comments()
+ {
+ echo 'Look at this!';
+ }
+ }
+ ?>
-<?php class Blog extends CI_Controller { public function index() { echo
-'Hello World!'; } public function comments() { echo 'Look at this!'; } }
-?>
Now load the following URL to see the comment function::
example.com/index.php/blog/comments/
@@ -97,7 +126,16 @@ For example, lets say you have a URI like this::
Your function will be passed URI segments 3 and 4 ("sandals" and "123")::
- <?php class Products extends CI_Controller {     public function shoes($sandals, $id)     {         echo $sandals;         echo $id;     } } ?>
+ <?php
+ class Products extends CI_Controller {
+
+ public function shoes($sandals, $id)
+ {
+ echo $sandals;
+ echo $id;
+ }
+ }
+ ?>
.. important:: If you are using the :doc:`URI Routing <routing>`
feature, the segments passed to your function will be the re-routed
@@ -124,7 +162,10 @@ As noted above, the second segment of the URI typically determines which
function in the controller gets called. CodeIgniter permits you to
override this behavior through the use of the _remap() function::
- public function _remap() {     // Some code here... }
+ public function _remap()
+ {
+ // Some code here...
+ }
.. important:: If your controller contains a function named _remap(),
it will **always** get called regardless of what your URI contains. It
@@ -134,7 +175,17 @@ override this behavior through the use of the _remap() function::
The overridden function call (typically the second segment of the URI)
will be passed as a parameter to the _remap() function::
- public function _remap($method) {     if ($method == 'some_method')     {         $this->$method();     }     else     {         $this->default_method();     } }
+ public function _remap($method)
+ {
+ if ($method == 'some_method')
+ {
+ $this->$method();
+ }
+ else
+ {
+ $this->default_method();
+ }
+ }
Any extra segments after the method name are passed into _remap() as an
optional second parameter. This array can be used in combination with
@@ -143,7 +194,15 @@ to emulate CodeIgniter's default behavior.
::
- public function _remap($method, $params = array()) {     $method = 'process_'.$method;     if (method_exists($this, $method))     {         return call_user_func_array(array($this, $method), $params);     }     show_404(); }
+ public function _remap($method, $params = array())
+ {
+ $method = 'process_'.$method;
+ if (method_exists($this, $method))
+ {
+ return call_user_func_array(array($this, $method), $params);
+ }
+ show_404();
+ }
Processing Output
=================
@@ -164,23 +223,29 @@ data.
Here is an example::
- public function _output($output) {     echo $output; }
-
-Please note that your _output() function will receive the data in its
-finalized state. Benchmark and memory usage data will be rendered, cache
-files written (if you have caching enabled), and headers will be sent
-(if you use that :doc:`feature <../libraries/output>`) before it is
-handed off to the _output() function.
-To have your controller's output cached properly, its _output() method
-can use::
-
- if ($this->output->cache_expiration > 0) {     $this->output->_write_cache($output); }
-
-If you are using this feature the page execution timer and memory usage
-stats might not be perfectly accurate since they will not take into
-acccount any further processing you do. For an alternate way to control
-output *before* any of the final processing is done, please see the
-available methods in the :doc:`Output Class <../libraries/output>`.
+ public function _output($output)
+ {
+ echo $output;
+ }
+
+.. note:: Please note that your _output() function will receive the data in its
+ finalized state. Benchmark and memory usage data will be rendered, cache
+ files written (if you have caching enabled), and headers will be sent
+ (if you use that :doc:`feature <../libraries/output>`) before it is
+ handed off to the _output() function.
+ To have your controller's output cached properly, its _output() method
+ can use::
+
+ if ($this->output->cache_expiration > 0)
+ {
+ $this->output->_write_cache($output);
+ }
+
+ If you are using this feature the page execution timer and memory usage
+ stats might not be perfectly accurate since they will not take into
+ acccount any further processing you do. For an alternate way to control
+ output *before* any of the final processing is done, please see the
+ available methods in the :doc:`Output Class <../libraries/output>`.
Private Functions
=================
@@ -190,7 +255,10 @@ 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::
- private function _utility() {   // some code }
+ private function _utility()
+ {
+ // some code
+ }
Trying to access it via the URL, like this, will not work::
@@ -237,7 +305,16 @@ manually call it.
::
- <?php class Blog extends CI_Controller {        public function __construct()        {             parent::__construct();             // Your own constructor code        } } ?>
+ <?php
+ class Blog extends CI_Controller {
+
+ public function __construct()
+ {
+ parent::__construct();
+ // Your own constructor code
+ }
+ }
+ ?>
Constructors are useful if you need to set some default values, or run a
default process when your class is instantiated. Constructors can't