blob: 14e5836360fc9fafd47351bbb2b9f65b62a78a82 [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
Master Yoda12617cf2015-04-30 02:32:59 -0700141 $route['default_controller'] = 'blog';
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
Andrey Andreevee2ece82015-09-28 13:18:05 +0300143Where 'blog' is the name of the controller class you want used. If you now
Derek Jones8ede1a22011-10-05 13:34:52 -0500144load your main index.php file without specifying any URI segments you'll
Andrey Andreevee2ece82015-09-28 13:18:05 +0300145see your "Hello World" message by default.
146
147For more information, please refer to the "Reserved Routes" section of the
Andrey Andreevc094bec2015-10-08 17:18:57 +0300148:doc:`URI Routing <routing>` documentation.
Derek Jones8ede1a22011-10-05 13:34:52 -0500149
Andrey Andreev522c7362012-11-05 16:40:32 +0200150Remapping Method Calls
151======================
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
153As noted above, the second segment of the URI typically determines which
Andrey Andreev522c7362012-11-05 16:40:32 +0200154method in the controller gets called. CodeIgniter permits you to override
155this behavior through the use of the ``_remap()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Derek Jonese69b4562011-10-05 17:30:50 -0500157 public function _remap()
158 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200159 // Some code here...
Derek Jonese69b4562011-10-05 17:30:50 -0500160 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
Andrey Andreev522c7362012-11-05 16:40:32 +0200162.. important:: If your controller contains a method named _remap(),
Derek Jones8ede1a22011-10-05 13:34:52 -0500163 it will **always** get called regardless of what your URI contains. It
Andrey Andreev522c7362012-11-05 16:40:32 +0200164 overrides the normal behavior in which the URI determines which method
165 is called, allowing you to define your own method routing rules.
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Andrey Andreev522c7362012-11-05 16:40:32 +0200167The overridden method call (typically the second segment of the URI) will
168be passed as a parameter to the ``_remap()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
Derek Jonese69b4562011-10-05 17:30:50 -0500170 public function _remap($method)
171 {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200172 if ($method === 'some_method')
Andrey Andreev522c7362012-11-05 16:40:32 +0200173 {
174 $this->$method();
175 }
176 else
177 {
178 $this->default_method();
179 }
Derek Jonese69b4562011-10-05 17:30:50 -0500180 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500181
Andrey Andreev522c7362012-11-05 16:40:32 +0200182Any extra segments after the method name are passed into ``_remap()`` as an
Derek Jones8ede1a22011-10-05 13:34:52 -0500183optional second parameter. This array can be used in combination with
Andrey Andreev522c7362012-11-05 16:40:32 +0200184PHP's `call_user_func_array() <http://php.net/call_user_func_array>`_
Derek Jones8ede1a22011-10-05 13:34:52 -0500185to emulate CodeIgniter's default behavior.
186
Andrey Andreev16a704c2012-11-09 17:25:00 +0200187Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
Derek Jonese69b4562011-10-05 17:30:50 -0500189 public function _remap($method, $params = array())
190 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200191 $method = 'process_'.$method;
192 if (method_exists($this, $method))
193 {
194 return call_user_func_array(array($this, $method), $params);
195 }
196 show_404();
Derek Jonese69b4562011-10-05 17:30:50 -0500197 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
199Processing Output
200=================
201
202CodeIgniter has an output class that takes care of sending your final
203rendered data to the web browser automatically. More information on this
Andrey Andreev522c7362012-11-05 16:40:32 +0200204can be found in the :doc:`Views <views>` and :doc:`Output Class
205<../libraries/output>` pages. In some cases, however, you might want to
206post-process the finalized data in some way and send it to the browser
207yourself. CodeIgniter permits you to add a method named ``_output()``
208to your controller that will receive the finalized output data.
Derek Jones8ede1a22011-10-05 13:34:52 -0500209
Andrey Andreev16a704c2012-11-09 17:25:00 +0200210.. important:: If your controller contains a method named ``_output()``,
211 it will **always** be called by the output class instead of
212 echoing the finalized data directly. The first parameter of the
213 method will contain the finalized output.
Derek Jones8ede1a22011-10-05 13:34:52 -0500214
215Here is an example::
216
Derek Jonese69b4562011-10-05 17:30:50 -0500217 public function _output($output)
218 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200219 echo $output;
Derek Jonese69b4562011-10-05 17:30:50 -0500220 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500221
Kevin Smithd3f9efe2013-05-13 16:34:37 -0500222.. note::
223
224 Please note that your ``_output()`` method will receive the
Andrey Andreev16a704c2012-11-09 17:25:00 +0200225 data in its finalized state. Benchmark and memory usage data
226 will be rendered, cache files written (if you have caching
227 enabled), and headers will be sent (if you use that
228 :doc:`feature <../libraries/output>`) before it is handed off
229 to the ``_output()`` method.
230 To have your controller's output cached properly, its
231 ``_output()`` method can use::
Derek Jones8ede1a22011-10-05 13:34:52 -0500232
Derek Jonese69b4562011-10-05 17:30:50 -0500233 if ($this->output->cache_expiration > 0)
234 {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200235 $this->output->_write_cache($output);
Derek Jonese69b4562011-10-05 17:30:50 -0500236 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500237
Andrey Andreev16a704c2012-11-09 17:25:00 +0200238 If you are using this feature the page execution timer and
239 memory usage stats might not be perfectly accurate since they
240 will not take into account any further processing you do.
241 For an alternate way to control output *before* any of the
242 final processing is done, please see the available methods
243 in the :doc:`Output Library <../libraries/output>`.
Derek Jones8ede1a22011-10-05 13:34:52 -0500244
Andrey Andreev522c7362012-11-05 16:40:32 +0200245Private methods
246===============
Derek Jones8ede1a22011-10-05 13:34:52 -0500247
Andrey Andreev522c7362012-11-05 16:40:32 +0200248In some cases you may want certain methods hidden from public access.
249In order to achieve this, simply declare the method as being private
250or protected and it will not be served via a URL request. For example,
251if you were to have a method like this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500252
Derek Jonese69b4562011-10-05 17:30:50 -0500253 private function _utility()
254 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200255 // some code
Derek Jonese69b4562011-10-05 17:30:50 -0500256 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500257
258Trying to access it via the URL, like this, will not work::
259
260 example.com/index.php/blog/_utility/
261
Andrey Andreev522c7362012-11-05 16:40:32 +0200262.. note:: Prefixing method names with an underscore will also prevent
263 them from being called. This is a legacy feature that is left
264 for backwards-compatibility.
265
Andrey Andreev16a704c2012-11-09 17:25:00 +0200266Organizing Your Controllers into Sub-directories
267================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500268
Andrey Andreevee2ece82015-09-28 13:18:05 +0300269If you are building a large application you might want to hierarchically
270organize or structure your controllers into sub-directories. CodeIgniter
271permits you to do this.
Derek Jones8ede1a22011-10-05 13:34:52 -0500272
Andrey Andreevee2ece82015-09-28 13:18:05 +0300273Simply create sub-directories under the main *application/controllers/*
274one and place your controller classes within them.
Derek Jones8ede1a22011-10-05 13:34:52 -0500275
276.. note:: When using this feature the first segment of your URI must
vlakoff35672462013-02-15 01:36:04 +0100277 specify the folder. For example, let's say you have a controller located
Derek Jones8ede1a22011-10-05 13:34:52 -0500278 here::
279
Andrey Andreev20292312013-07-22 14:29:10 +0300280 application/controllers/products/Shoes.php
Derek Jones8ede1a22011-10-05 13:34:52 -0500281
282 To call the above controller your URI will look something like this::
283
284 example.com/index.php/products/shoes/show/123
285
Andrey Andreev16a704c2012-11-09 17:25:00 +0200286Each of your sub-directories may contain a default controller which will be
Andrey Andreevee2ece82015-09-28 13:18:05 +0300287called if the URL contains *only* the sub-directory. Simply put a controller
288in there that matches the name of your 'default_controller' as specified in
289your *application/config/routes.php* file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500290
291CodeIgniter also permits you to remap your URIs using its :doc:`URI
292Routing <routing>` feature.
293
294Class Constructors
295==================
296
297If you intend to use a constructor in any of your Controllers, you
298**MUST** place the following line of code in it::
299
300 parent::__construct();
301
302The reason this line is necessary is because your local constructor will
303be overriding the one in the parent controller class so we need to
304manually call it.
305
Andrey Andreev16a704c2012-11-09 17:25:00 +0200306Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500307
Derek Jonese69b4562011-10-05 17:30:50 -0500308 <?php
309 class Blog extends CI_Controller {
310
Andrey Andreev522c7362012-11-05 16:40:32 +0200311 public function __construct()
312 {
313 parent::__construct();
314 // Your own constructor code
315 }
Derek Jonese69b4562011-10-05 17:30:50 -0500316 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500317
318Constructors are useful if you need to set some default values, or run a
319default process when your class is instantiated. Constructors can't
320return a value, but they can do some default work.
321
Andrey Andreev522c7362012-11-05 16:40:32 +0200322Reserved method names
323=====================
Derek Jones8ede1a22011-10-05 13:34:52 -0500324
325Since your controller classes will extend the main application
Andrey Andreev522c7362012-11-05 16:40:32 +0200326controller you must be careful not to name your methods identically to
Derek Jones8ede1a22011-10-05 13:34:52 -0500327the ones used by that class, otherwise your local functions will
328override them. See :doc:`Reserved Names <reserved_names>` for a full
329list.
330
Andrey Andreev522c7362012-11-05 16:40:32 +0200331.. important:: You should also never have a method named identically
Andrey Andreev16a704c2012-11-09 17:25:00 +0200332 to its class name. If you do, and there is no ``__construct()``
333 method in the same class, then your e.g. ``Index::index()``
334 method will be executed as a class constructor! This is a PHP4
Andrey Andreev522c7362012-11-05 16:40:32 +0200335 backwards-compatibility feature.
336
Derek Jones8ede1a22011-10-05 13:34:52 -0500337That's it!
338==========
339
Andrey Andreev4a567782017-11-20 09:58:03 +0200340That, in a nutshell, is all there is to know about controllers.