blob: 729b08417f0b5d550e8b77ab9e7a756e1d615440 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###########
2Controllers
3###########
4
5Controllers are the heart of your application, as they determine how
6HTTP requests should be handled.
7
Joseph Wensley5b3ea1a2011-10-06 20:54:32 -04008.. contents:: Page Contents
Derek Jones8ede1a22011-10-05 13:34:52 -05009
10What is a Controller?
11=====================
12
purwandi02df61f2011-10-07 15:33:40 +070013**A Controller is simply a class file that is named in a way that can be
14associated with a URI.**
Derek Jones8ede1a22011-10-05 13:34:52 -050015
16Consider this URI::
17
18 example.com/index.php/blog/
19
20In the above example, CodeIgniter would attempt to find a controller
21named blog.php and load it.
22
23**When a controller's name matches the first segment of a URI, it will
24be loaded.**
25
26Let's try it: Hello World!
27==========================
28
29Let's create a simple controller so you can see it in action. Using your
30text editor, create a file called blog.php, and put the following code
Derek Jonese69b4562011-10-05 17:30:50 -050031in it::
Derek Jones8ede1a22011-10-05 13:34:52 -050032
Derek Jonese69b4562011-10-05 17:30:50 -050033 <?php
34 class Blog extends CI_Controller {
35
36 public function index()
37 {
38 echo 'Hello World!';
39 }
40 }
Derek Jonese69b4562011-10-05 17:30:50 -050041
Andrey Andreev16a704c2012-11-09 17:25:00 +020042Then save the file to your *application/controllers/* directory.
Derek Jones8ede1a22011-10-05 13:34:52 -050043
44Now visit the your site using a URL similar to this::
45
46 example.com/index.php/blog/
47
Andrey Andreev16a704c2012-11-09 17:25:00 +020048If you did it right, you should see:
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Andrey Andreev16a704c2012-11-09 17:25:00 +020050 Hello World!
51
52.. important:: Class names must start with an uppercase letter.
53
54This is valid::
Derek Jones8ede1a22011-10-05 13:34:52 -050055
Derek Jonese69b4562011-10-05 17:30:50 -050056 <?php
57 class Blog extends CI_Controller {
58
59 }
Derek Jonese69b4562011-10-05 17:30:50 -050060
Derek Jones8ede1a22011-10-05 13:34:52 -050061This is **not** valid::
62
Derek Jonese69b4562011-10-05 17:30:50 -050063 <?php
64 class blog extends CI_Controller {
65
66 }
Derek Jones8ede1a22011-10-05 13:34:52 -050067
68Also, always make sure your controller extends the parent controller
Andrey Andreev522c7362012-11-05 16:40:32 +020069class so that it can inherit all its methods.
Derek Jones8ede1a22011-10-05 13:34:52 -050070
Andrey Andreev522c7362012-11-05 16:40:32 +020071Methods
72=======
Derek Jones8ede1a22011-10-05 13:34:52 -050073
Andrey Andreev522c7362012-11-05 16:40:32 +020074In the above example the method name is ``index()``. The "index" method
Derek Jones8ede1a22011-10-05 13:34:52 -050075is always loaded by default if the **second segment** of the URI is
76empty. Another way to show your "Hello World" message would be this::
77
78 example.com/index.php/blog/index/
79
Andrey Andreev522c7362012-11-05 16:40:32 +020080**The second segment of the URI determines which method in the
Derek Jones8ede1a22011-10-05 13:34:52 -050081controller gets called.**
82
Andrey Andreev522c7362012-11-05 16:40:32 +020083Let's try it. Add a new method to your controller::
Derek Jones8ede1a22011-10-05 13:34:52 -050084
Derek Jonese69b4562011-10-05 17:30:50 -050085 <?php
86 class Blog extends CI_Controller {
87
88 public function index()
89 {
90 echo 'Hello World!';
91 }
92
93 public function comments()
94 {
95 echo 'Look at this!';
96 }
97 }
Derek Jonese69b4562011-10-05 17:30:50 -050098
Andrey Andreev522c7362012-11-05 16:40:32 +020099Now load the following URL to see the comment method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
101 example.com/index.php/blog/comments/
102
103You should see your new message.
104
Andrey Andreev522c7362012-11-05 16:40:32 +0200105Passing URI Segments to your methods
106====================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
108If your URI contains more then two segments they will be passed to your
Andrey Andreev522c7362012-11-05 16:40:32 +0200109method as parameters.
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
111For example, lets say you have a URI like this::
112
113 example.com/index.php/products/shoes/sandals/123
114
Andrey Andreev522c7362012-11-05 16:40:32 +0200115Your method will be passed URI segments 3 and 4 ("sandals" and "123")::
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Derek Jonese69b4562011-10-05 17:30:50 -0500117 <?php
118 class Products extends CI_Controller {
119
Andrey Andreev522c7362012-11-05 16:40:32 +0200120 public function shoes($sandals, $id)
121 {
122 echo $sandals;
123 echo $id;
124 }
Derek Jonese69b4562011-10-05 17:30:50 -0500125 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
127.. important:: If you are using the :doc:`URI Routing <routing>`
Andrey Andreev522c7362012-11-05 16:40:32 +0200128 feature, the segments passed to your method will be the re-routed
Derek Jones8ede1a22011-10-05 13:34:52 -0500129 ones.
130
131Defining a Default Controller
132=============================
133
134CodeIgniter can be told to load a default controller when a URI is not
135present, as will be the case when only your site root URL is requested.
purwandi02df61f2011-10-07 15:33:40 +0700136To specify a default controller, open your **application/config/routes.php**
Derek Jones8ede1a22011-10-05 13:34:52 -0500137file and set this variable::
138
Andrey Andreev16a704c2012-11-09 17:25:00 +0200139 $route['default_controller'] = 'blog';
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
141Where Blog is the name of the controller class you want used. If you now
142load your main index.php file without specifying any URI segments you'll
143see your Hello World message by default.
144
Andrey Andreev522c7362012-11-05 16:40:32 +0200145Remapping Method Calls
146======================
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
148As noted above, the second segment of the URI typically determines which
Andrey Andreev522c7362012-11-05 16:40:32 +0200149method in the controller gets called. CodeIgniter permits you to override
150this behavior through the use of the ``_remap()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500151
Derek Jonese69b4562011-10-05 17:30:50 -0500152 public function _remap()
153 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200154 // Some code here...
Derek Jonese69b4562011-10-05 17:30:50 -0500155 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Andrey Andreev522c7362012-11-05 16:40:32 +0200157.. important:: If your controller contains a method named _remap(),
Derek Jones8ede1a22011-10-05 13:34:52 -0500158 it will **always** get called regardless of what your URI contains. It
Andrey Andreev522c7362012-11-05 16:40:32 +0200159 overrides the normal behavior in which the URI determines which method
160 is called, allowing you to define your own method routing rules.
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
Andrey Andreev522c7362012-11-05 16:40:32 +0200162The overridden method call (typically the second segment of the URI) will
163be passed as a parameter to the ``_remap()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Derek Jonese69b4562011-10-05 17:30:50 -0500165 public function _remap($method)
166 {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200167 if ($method === 'some_method')
Andrey Andreev522c7362012-11-05 16:40:32 +0200168 {
169 $this->$method();
170 }
171 else
172 {
173 $this->default_method();
174 }
Derek Jonese69b4562011-10-05 17:30:50 -0500175 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
Andrey Andreev522c7362012-11-05 16:40:32 +0200177Any extra segments after the method name are passed into ``_remap()`` as an
Derek Jones8ede1a22011-10-05 13:34:52 -0500178optional second parameter. This array can be used in combination with
Andrey Andreev522c7362012-11-05 16:40:32 +0200179PHP's `call_user_func_array() <http://php.net/call_user_func_array>`_
Derek Jones8ede1a22011-10-05 13:34:52 -0500180to emulate CodeIgniter's default behavior.
181
Andrey Andreev16a704c2012-11-09 17:25:00 +0200182Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
Derek Jonese69b4562011-10-05 17:30:50 -0500184 public function _remap($method, $params = array())
185 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200186 $method = 'process_'.$method;
187 if (method_exists($this, $method))
188 {
189 return call_user_func_array(array($this, $method), $params);
190 }
191 show_404();
Derek Jonese69b4562011-10-05 17:30:50 -0500192 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500193
194Processing Output
195=================
196
197CodeIgniter has an output class that takes care of sending your final
198rendered data to the web browser automatically. More information on this
Andrey Andreev522c7362012-11-05 16:40:32 +0200199can be found in the :doc:`Views <views>` and :doc:`Output Class
200<../libraries/output>` pages. In some cases, however, you might want to
201post-process the finalized data in some way and send it to the browser
202yourself. CodeIgniter permits you to add a method named ``_output()``
203to your controller that will receive the finalized output data.
Derek Jones8ede1a22011-10-05 13:34:52 -0500204
Andrey Andreev16a704c2012-11-09 17:25:00 +0200205.. important:: If your controller contains a method named ``_output()``,
206 it will **always** be called by the output class instead of
207 echoing the finalized data directly. The first parameter of the
208 method will contain the finalized output.
Derek Jones8ede1a22011-10-05 13:34:52 -0500209
210Here is an example::
211
Derek Jonese69b4562011-10-05 17:30:50 -0500212 public function _output($output)
213 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200214 echo $output;
Derek Jonese69b4562011-10-05 17:30:50 -0500215 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500216
Andrey Andreev16a704c2012-11-09 17:25:00 +0200217.. note:: Please note that your ``_output()`` method will receive the
218 data in its finalized state. Benchmark and memory usage data
219 will be rendered, cache files written (if you have caching
220 enabled), and headers will be sent (if you use that
221 :doc:`feature <../libraries/output>`) before it is handed off
222 to the ``_output()`` method.
223 To have your controller's output cached properly, its
224 ``_output()`` method can use::
Derek Jones8ede1a22011-10-05 13:34:52 -0500225
Derek Jonese69b4562011-10-05 17:30:50 -0500226 if ($this->output->cache_expiration > 0)
227 {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200228 $this->output->_write_cache($output);
Derek Jonese69b4562011-10-05 17:30:50 -0500229 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500230
Andrey Andreev16a704c2012-11-09 17:25:00 +0200231 If you are using this feature the page execution timer and
232 memory usage stats might not be perfectly accurate since they
233 will not take into account any further processing you do.
234 For an alternate way to control output *before* any of the
235 final processing is done, please see the available methods
236 in the :doc:`Output Library <../libraries/output>`.
Derek Jones8ede1a22011-10-05 13:34:52 -0500237
Andrey Andreev522c7362012-11-05 16:40:32 +0200238Private methods
239===============
Derek Jones8ede1a22011-10-05 13:34:52 -0500240
Andrey Andreev522c7362012-11-05 16:40:32 +0200241In some cases you may want certain methods hidden from public access.
242In order to achieve this, simply declare the method as being private
243or protected and it will not be served via a URL request. For example,
244if you were to have a method like this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500245
Derek Jonese69b4562011-10-05 17:30:50 -0500246 private function _utility()
247 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200248 // some code
Derek Jonese69b4562011-10-05 17:30:50 -0500249 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500250
251Trying to access it via the URL, like this, will not work::
252
253 example.com/index.php/blog/_utility/
254
Andrey Andreev522c7362012-11-05 16:40:32 +0200255.. note:: Prefixing method names with an underscore will also prevent
256 them from being called. This is a legacy feature that is left
257 for backwards-compatibility.
258
Andrey Andreev16a704c2012-11-09 17:25:00 +0200259Organizing Your Controllers into Sub-directories
260================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500261
262If you are building a large application you might find it convenient to
Andrey Andreev16a704c2012-11-09 17:25:00 +0200263organize your controllers into sub-directories. CodeIgniter permits you
264to do this.
Derek Jones8ede1a22011-10-05 13:34:52 -0500265
Andrey Andreev16a704c2012-11-09 17:25:00 +0200266Simply create folders within your *application/controllers/* directory
267and place your controller classes within them.
Derek Jones8ede1a22011-10-05 13:34:52 -0500268
269.. note:: When using this feature the first segment of your URI must
270 specify the folder. For example, lets say you have a controller located
271 here::
272
273 application/controllers/products/shoes.php
274
275 To call the above controller your URI will look something like this::
276
277 example.com/index.php/products/shoes/show/123
278
Andrey Andreev16a704c2012-11-09 17:25:00 +0200279Each of your sub-directories may contain a default controller which will be
Derek Jones8ede1a22011-10-05 13:34:52 -0500280called if the URL contains only the sub-folder. Simply name your default
Andrey Andreev16a704c2012-11-09 17:25:00 +0200281controller as specified in your *application/config/routes.php* file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500282
283CodeIgniter also permits you to remap your URIs using its :doc:`URI
284Routing <routing>` feature.
285
286Class Constructors
287==================
288
289If you intend to use a constructor in any of your Controllers, you
290**MUST** place the following line of code in it::
291
292 parent::__construct();
293
294The reason this line is necessary is because your local constructor will
295be overriding the one in the parent controller class so we need to
296manually call it.
297
Andrey Andreev16a704c2012-11-09 17:25:00 +0200298Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500299
Derek Jonese69b4562011-10-05 17:30:50 -0500300 <?php
301 class Blog extends CI_Controller {
302
Andrey Andreev522c7362012-11-05 16:40:32 +0200303 public function __construct()
304 {
305 parent::__construct();
306 // Your own constructor code
307 }
Derek Jonese69b4562011-10-05 17:30:50 -0500308 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500309
310Constructors are useful if you need to set some default values, or run a
311default process when your class is instantiated. Constructors can't
312return a value, but they can do some default work.
313
Andrey Andreev522c7362012-11-05 16:40:32 +0200314Reserved method names
315=====================
Derek Jones8ede1a22011-10-05 13:34:52 -0500316
317Since your controller classes will extend the main application
Andrey Andreev522c7362012-11-05 16:40:32 +0200318controller you must be careful not to name your methods identically to
Derek Jones8ede1a22011-10-05 13:34:52 -0500319the ones used by that class, otherwise your local functions will
320override them. See :doc:`Reserved Names <reserved_names>` for a full
321list.
322
Andrey Andreev522c7362012-11-05 16:40:32 +0200323.. important:: You should also never have a method named identically
Andrey Andreev16a704c2012-11-09 17:25:00 +0200324 to its class name. If you do, and there is no ``__construct()``
325 method in the same class, then your e.g. ``Index::index()``
326 method will be executed as a class constructor! This is a PHP4
Andrey Andreev522c7362012-11-05 16:40:32 +0200327 backwards-compatibility feature.
328
Derek Jones8ede1a22011-10-05 13:34:52 -0500329That's it!
330==========
331
Andrey Andreev522c7362012-11-05 16:40:32 +0200332That, in a nutshell, is all there is to know about controllers.