blob: bc8319dd85c3eb72ed11e6038a745b9b19d68d5e [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
Andrey Andreev20292312013-07-22 14:29:10 +030021named Blog.php and load it.
Derek Jones8ede1a22011-10-05 13:34:52 -050022
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
Andrey Andreev20292312013-07-22 14:29:10 +030030text 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
Andrey Andreev20292312013-07-22 14:29:10 +030044.. important:: The file must be called 'Blog.php', with a capital 'B'.
45
Derek Jones8ede1a22011-10-05 13:34:52 -050046Now visit the your site using a URL similar to this::
47
48 example.com/index.php/blog/
49
Andrey Andreev16a704c2012-11-09 17:25:00 +020050If you did it right, you should see:
Derek Jones8ede1a22011-10-05 13:34:52 -050051
Andrey Andreev16a704c2012-11-09 17:25:00 +020052 Hello World!
53
54.. important:: Class names must start with an uppercase letter.
55
56This is valid::
Derek Jones8ede1a22011-10-05 13:34:52 -050057
Derek Jonese69b4562011-10-05 17:30:50 -050058 <?php
59 class Blog extends CI_Controller {
60
61 }
Derek Jonese69b4562011-10-05 17:30:50 -050062
Derek Jones8ede1a22011-10-05 13:34:52 -050063This is **not** valid::
64
Derek Jonese69b4562011-10-05 17:30:50 -050065 <?php
66 class blog extends CI_Controller {
67
68 }
Derek Jones8ede1a22011-10-05 13:34:52 -050069
70Also, always make sure your controller extends the parent controller
Andrey Andreev522c7362012-11-05 16:40:32 +020071class so that it can inherit all its methods.
Derek Jones8ede1a22011-10-05 13:34:52 -050072
Andrey Andreev522c7362012-11-05 16:40:32 +020073Methods
74=======
Derek Jones8ede1a22011-10-05 13:34:52 -050075
Andrey Andreev522c7362012-11-05 16:40:32 +020076In the above example the method name is ``index()``. The "index" method
Derek Jones8ede1a22011-10-05 13:34:52 -050077is always loaded by default if the **second segment** of the URI is
78empty. Another way to show your "Hello World" message would be this::
79
80 example.com/index.php/blog/index/
81
Andrey Andreev522c7362012-11-05 16:40:32 +020082**The second segment of the URI determines which method in the
Derek Jones8ede1a22011-10-05 13:34:52 -050083controller gets called.**
84
Andrey Andreev522c7362012-11-05 16:40:32 +020085Let's try it. Add a new method to your controller::
Derek Jones8ede1a22011-10-05 13:34:52 -050086
Derek Jonese69b4562011-10-05 17:30:50 -050087 <?php
88 class Blog extends CI_Controller {
89
90 public function index()
91 {
92 echo 'Hello World!';
93 }
94
95 public function comments()
96 {
97 echo 'Look at this!';
98 }
99 }
Derek Jonese69b4562011-10-05 17:30:50 -0500100
Andrey Andreev522c7362012-11-05 16:40:32 +0200101Now load the following URL to see the comment method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500102
103 example.com/index.php/blog/comments/
104
105You should see your new message.
106
Andrey Andreev522c7362012-11-05 16:40:32 +0200107Passing URI Segments to your methods
108====================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
Andrey Andreevba231aa2014-01-20 16:43:41 +0200110If your URI contains more than two segments they will be passed to your
Andrey Andreev522c7362012-11-05 16:40:32 +0200111method as parameters.
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
vlakoff35672462013-02-15 01:36:04 +0100113For example, let's say you have a URI like this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
115 example.com/index.php/products/shoes/sandals/123
116
Andrey Andreev522c7362012-11-05 16:40:32 +0200117Your method will be passed URI segments 3 and 4 ("sandals" and "123")::
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
Derek Jonese69b4562011-10-05 17:30:50 -0500119 <?php
120 class Products extends CI_Controller {
121
Andrey Andreev522c7362012-11-05 16:40:32 +0200122 public function shoes($sandals, $id)
123 {
124 echo $sandals;
125 echo $id;
126 }
Derek Jonese69b4562011-10-05 17:30:50 -0500127 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
129.. important:: If you are using the :doc:`URI Routing <routing>`
Andrey Andreev522c7362012-11-05 16:40:32 +0200130 feature, the segments passed to your method will be the re-routed
Derek Jones8ede1a22011-10-05 13:34:52 -0500131 ones.
132
133Defining a Default Controller
134=============================
135
136CodeIgniter can be told to load a default controller when a URI is not
137present, as will be the case when only your site root URL is requested.
purwandi02df61f2011-10-07 15:33:40 +0700138To specify a default controller, open your **application/config/routes.php**
Derek Jones8ede1a22011-10-05 13:34:52 -0500139file and set this variable::
140
Andrey Andreev20292312013-07-22 14:29:10 +0300141 $route['default_controller'] = 'Blog';
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
143Where Blog is the name of the controller class you want used. If you now
144load your main index.php file without specifying any URI segments you'll
145see your Hello World message by default.
146
Andrey Andreev522c7362012-11-05 16:40:32 +0200147Remapping Method Calls
148======================
Derek Jones8ede1a22011-10-05 13:34:52 -0500149
150As noted above, the second segment of the URI typically determines which
Andrey Andreev522c7362012-11-05 16:40:32 +0200151method in the controller gets called. CodeIgniter permits you to override
152this behavior through the use of the ``_remap()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
Derek Jonese69b4562011-10-05 17:30:50 -0500154 public function _remap()
155 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200156 // Some code here...
Derek Jonese69b4562011-10-05 17:30:50 -0500157 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
Andrey Andreev522c7362012-11-05 16:40:32 +0200159.. important:: If your controller contains a method named _remap(),
Derek Jones8ede1a22011-10-05 13:34:52 -0500160 it will **always** get called regardless of what your URI contains. It
Andrey Andreev522c7362012-11-05 16:40:32 +0200161 overrides the normal behavior in which the URI determines which method
162 is called, allowing you to define your own method routing rules.
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Andrey Andreev522c7362012-11-05 16:40:32 +0200164The overridden method call (typically the second segment of the URI) will
165be passed as a parameter to the ``_remap()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Derek Jonese69b4562011-10-05 17:30:50 -0500167 public function _remap($method)
168 {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200169 if ($method === 'some_method')
Andrey Andreev522c7362012-11-05 16:40:32 +0200170 {
171 $this->$method();
172 }
173 else
174 {
175 $this->default_method();
176 }
Derek Jonese69b4562011-10-05 17:30:50 -0500177 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500178
Andrey Andreev522c7362012-11-05 16:40:32 +0200179Any extra segments after the method name are passed into ``_remap()`` as an
Derek Jones8ede1a22011-10-05 13:34:52 -0500180optional second parameter. This array can be used in combination with
Andrey Andreev522c7362012-11-05 16:40:32 +0200181PHP's `call_user_func_array() <http://php.net/call_user_func_array>`_
Derek Jones8ede1a22011-10-05 13:34:52 -0500182to emulate CodeIgniter's default behavior.
183
Andrey Andreev16a704c2012-11-09 17:25:00 +0200184Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
Derek Jonese69b4562011-10-05 17:30:50 -0500186 public function _remap($method, $params = array())
187 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200188 $method = 'process_'.$method;
189 if (method_exists($this, $method))
190 {
191 return call_user_func_array(array($this, $method), $params);
192 }
193 show_404();
Derek Jonese69b4562011-10-05 17:30:50 -0500194 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500195
196Processing Output
197=================
198
199CodeIgniter has an output class that takes care of sending your final
200rendered data to the web browser automatically. More information on this
Andrey Andreev522c7362012-11-05 16:40:32 +0200201can be found in the :doc:`Views <views>` and :doc:`Output Class
202<../libraries/output>` pages. In some cases, however, you might want to
203post-process the finalized data in some way and send it to the browser
204yourself. CodeIgniter permits you to add a method named ``_output()``
205to your controller that will receive the finalized output data.
Derek Jones8ede1a22011-10-05 13:34:52 -0500206
Andrey Andreev16a704c2012-11-09 17:25:00 +0200207.. important:: If your controller contains a method named ``_output()``,
208 it will **always** be called by the output class instead of
209 echoing the finalized data directly. The first parameter of the
210 method will contain the finalized output.
Derek Jones8ede1a22011-10-05 13:34:52 -0500211
212Here is an example::
213
Derek Jonese69b4562011-10-05 17:30:50 -0500214 public function _output($output)
215 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200216 echo $output;
Derek Jonese69b4562011-10-05 17:30:50 -0500217 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500218
Kevin Smithd3f9efe2013-05-13 16:34:37 -0500219.. note::
220
221 Please note that your ``_output()`` method will receive the
Andrey Andreev16a704c2012-11-09 17:25:00 +0200222 data in its finalized state. Benchmark and memory usage data
223 will be rendered, cache files written (if you have caching
224 enabled), and headers will be sent (if you use that
225 :doc:`feature <../libraries/output>`) before it is handed off
226 to the ``_output()`` method.
227 To have your controller's output cached properly, its
228 ``_output()`` method can use::
Derek Jones8ede1a22011-10-05 13:34:52 -0500229
Derek Jonese69b4562011-10-05 17:30:50 -0500230 if ($this->output->cache_expiration > 0)
231 {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200232 $this->output->_write_cache($output);
Derek Jonese69b4562011-10-05 17:30:50 -0500233 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500234
Andrey Andreev16a704c2012-11-09 17:25:00 +0200235 If you are using this feature the page execution timer and
236 memory usage stats might not be perfectly accurate since they
237 will not take into account any further processing you do.
238 For an alternate way to control output *before* any of the
239 final processing is done, please see the available methods
240 in the :doc:`Output Library <../libraries/output>`.
Derek Jones8ede1a22011-10-05 13:34:52 -0500241
Andrey Andreev522c7362012-11-05 16:40:32 +0200242Private methods
243===============
Derek Jones8ede1a22011-10-05 13:34:52 -0500244
Andrey Andreev522c7362012-11-05 16:40:32 +0200245In some cases you may want certain methods hidden from public access.
246In order to achieve this, simply declare the method as being private
247or protected and it will not be served via a URL request. For example,
248if you were to have a method like this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500249
Derek Jonese69b4562011-10-05 17:30:50 -0500250 private function _utility()
251 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200252 // some code
Derek Jonese69b4562011-10-05 17:30:50 -0500253 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500254
255Trying to access it via the URL, like this, will not work::
256
257 example.com/index.php/blog/_utility/
258
Andrey Andreev522c7362012-11-05 16:40:32 +0200259.. note:: Prefixing method names with an underscore will also prevent
260 them from being called. This is a legacy feature that is left
261 for backwards-compatibility.
262
Andrey Andreev16a704c2012-11-09 17:25:00 +0200263Organizing Your Controllers into Sub-directories
264================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500265
266If you are building a large application you might find it convenient to
Andrey Andreev16a704c2012-11-09 17:25:00 +0200267organize your controllers into sub-directories. CodeIgniter permits you
268to do this.
Derek Jones8ede1a22011-10-05 13:34:52 -0500269
Andrey Andreev16a704c2012-11-09 17:25:00 +0200270Simply create folders within your *application/controllers/* directory
271and place your controller classes within them.
Derek Jones8ede1a22011-10-05 13:34:52 -0500272
273.. note:: When using this feature the first segment of your URI must
vlakoff35672462013-02-15 01:36:04 +0100274 specify the folder. For example, let's say you have a controller located
Derek Jones8ede1a22011-10-05 13:34:52 -0500275 here::
276
Andrey Andreev20292312013-07-22 14:29:10 +0300277 application/controllers/products/Shoes.php
Derek Jones8ede1a22011-10-05 13:34:52 -0500278
279 To call the above controller your URI will look something like this::
280
281 example.com/index.php/products/shoes/show/123
282
Andrey Andreev16a704c2012-11-09 17:25:00 +0200283Each of your sub-directories may contain a default controller which will be
Derek Jones8ede1a22011-10-05 13:34:52 -0500284called if the URL contains only the sub-folder. Simply name your default
Andrey Andreev16a704c2012-11-09 17:25:00 +0200285controller as specified in your *application/config/routes.php* file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500286
287CodeIgniter also permits you to remap your URIs using its :doc:`URI
288Routing <routing>` feature.
289
290Class Constructors
291==================
292
293If you intend to use a constructor in any of your Controllers, you
294**MUST** place the following line of code in it::
295
296 parent::__construct();
297
298The reason this line is necessary is because your local constructor will
299be overriding the one in the parent controller class so we need to
300manually call it.
301
Andrey Andreev16a704c2012-11-09 17:25:00 +0200302Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500303
Derek Jonese69b4562011-10-05 17:30:50 -0500304 <?php
305 class Blog extends CI_Controller {
306
Andrey Andreev522c7362012-11-05 16:40:32 +0200307 public function __construct()
308 {
309 parent::__construct();
310 // Your own constructor code
311 }
Derek Jonese69b4562011-10-05 17:30:50 -0500312 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500313
314Constructors are useful if you need to set some default values, or run a
315default process when your class is instantiated. Constructors can't
316return a value, but they can do some default work.
317
Andrey Andreev522c7362012-11-05 16:40:32 +0200318Reserved method names
319=====================
Derek Jones8ede1a22011-10-05 13:34:52 -0500320
321Since your controller classes will extend the main application
Andrey Andreev522c7362012-11-05 16:40:32 +0200322controller you must be careful not to name your methods identically to
Derek Jones8ede1a22011-10-05 13:34:52 -0500323the ones used by that class, otherwise your local functions will
324override them. See :doc:`Reserved Names <reserved_names>` for a full
325list.
326
Andrey Andreev522c7362012-11-05 16:40:32 +0200327.. important:: You should also never have a method named identically
Andrey Andreev16a704c2012-11-09 17:25:00 +0200328 to its class name. If you do, and there is no ``__construct()``
329 method in the same class, then your e.g. ``Index::index()``
330 method will be executed as a class constructor! This is a PHP4
Andrey Andreev522c7362012-11-05 16:40:32 +0200331 backwards-compatibility feature.
332
Derek Jones8ede1a22011-10-05 13:34:52 -0500333That's it!
334==========
335
Andrey Andreev522c7362012-11-05 16:40:32 +0200336That, in a nutshell, is all there is to know about controllers.