blob: 04f23276d0a4e1453716c62fa9da5a4614e3fbcb [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
vlakoff35672462013-02-15 01:36:04 +0100111For example, let's say you have a URI like this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
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
Kevin Smithd3f9efe2013-05-13 16:34:37 -0500217.. note::
218
219 Please note that your ``_output()`` method will receive the
Andrey Andreev16a704c2012-11-09 17:25:00 +0200220 data in its finalized state. Benchmark and memory usage data
221 will be rendered, cache files written (if you have caching
222 enabled), and headers will be sent (if you use that
223 :doc:`feature <../libraries/output>`) before it is handed off
224 to the ``_output()`` method.
225 To have your controller's output cached properly, its
226 ``_output()`` method can use::
Derek Jones8ede1a22011-10-05 13:34:52 -0500227
Derek Jonese69b4562011-10-05 17:30:50 -0500228 if ($this->output->cache_expiration > 0)
229 {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200230 $this->output->_write_cache($output);
Derek Jonese69b4562011-10-05 17:30:50 -0500231 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500232
Andrey Andreev16a704c2012-11-09 17:25:00 +0200233 If you are using this feature the page execution timer and
234 memory usage stats might not be perfectly accurate since they
235 will not take into account any further processing you do.
236 For an alternate way to control output *before* any of the
237 final processing is done, please see the available methods
238 in the :doc:`Output Library <../libraries/output>`.
Derek Jones8ede1a22011-10-05 13:34:52 -0500239
Andrey Andreev522c7362012-11-05 16:40:32 +0200240Private methods
241===============
Derek Jones8ede1a22011-10-05 13:34:52 -0500242
Andrey Andreev522c7362012-11-05 16:40:32 +0200243In some cases you may want certain methods hidden from public access.
244In order to achieve this, simply declare the method as being private
245or protected and it will not be served via a URL request. For example,
246if you were to have a method like this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500247
Derek Jonese69b4562011-10-05 17:30:50 -0500248 private function _utility()
249 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200250 // some code
Derek Jonese69b4562011-10-05 17:30:50 -0500251 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500252
253Trying to access it via the URL, like this, will not work::
254
255 example.com/index.php/blog/_utility/
256
Andrey Andreev522c7362012-11-05 16:40:32 +0200257.. note:: Prefixing method names with an underscore will also prevent
258 them from being called. This is a legacy feature that is left
259 for backwards-compatibility.
260
Andrey Andreev16a704c2012-11-09 17:25:00 +0200261Organizing Your Controllers into Sub-directories
262================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500263
264If you are building a large application you might find it convenient to
Andrey Andreev16a704c2012-11-09 17:25:00 +0200265organize your controllers into sub-directories. CodeIgniter permits you
266to do this.
Derek Jones8ede1a22011-10-05 13:34:52 -0500267
Andrey Andreev16a704c2012-11-09 17:25:00 +0200268Simply create folders within your *application/controllers/* directory
269and place your controller classes within them.
Derek Jones8ede1a22011-10-05 13:34:52 -0500270
271.. note:: When using this feature the first segment of your URI must
vlakoff35672462013-02-15 01:36:04 +0100272 specify the folder. For example, let's say you have a controller located
Derek Jones8ede1a22011-10-05 13:34:52 -0500273 here::
274
275 application/controllers/products/shoes.php
276
277 To call the above controller your URI will look something like this::
278
279 example.com/index.php/products/shoes/show/123
280
Andrey Andreev16a704c2012-11-09 17:25:00 +0200281Each of your sub-directories may contain a default controller which will be
Derek Jones8ede1a22011-10-05 13:34:52 -0500282called if the URL contains only the sub-folder. Simply name your default
Andrey Andreev16a704c2012-11-09 17:25:00 +0200283controller as specified in your *application/config/routes.php* file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500284
285CodeIgniter also permits you to remap your URIs using its :doc:`URI
286Routing <routing>` feature.
287
288Class Constructors
289==================
290
291If you intend to use a constructor in any of your Controllers, you
292**MUST** place the following line of code in it::
293
294 parent::__construct();
295
296The reason this line is necessary is because your local constructor will
297be overriding the one in the parent controller class so we need to
298manually call it.
299
Andrey Andreev16a704c2012-11-09 17:25:00 +0200300Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500301
Derek Jonese69b4562011-10-05 17:30:50 -0500302 <?php
303 class Blog extends CI_Controller {
304
Andrey Andreev522c7362012-11-05 16:40:32 +0200305 public function __construct()
306 {
307 parent::__construct();
308 // Your own constructor code
309 }
Derek Jonese69b4562011-10-05 17:30:50 -0500310 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500311
312Constructors are useful if you need to set some default values, or run a
313default process when your class is instantiated. Constructors can't
314return a value, but they can do some default work.
315
Andrey Andreev522c7362012-11-05 16:40:32 +0200316Reserved method names
317=====================
Derek Jones8ede1a22011-10-05 13:34:52 -0500318
319Since your controller classes will extend the main application
Andrey Andreev522c7362012-11-05 16:40:32 +0200320controller you must be careful not to name your methods identically to
Derek Jones8ede1a22011-10-05 13:34:52 -0500321the ones used by that class, otherwise your local functions will
322override them. See :doc:`Reserved Names <reserved_names>` for a full
323list.
324
Andrey Andreev522c7362012-11-05 16:40:32 +0200325.. important:: You should also never have a method named identically
Andrey Andreev16a704c2012-11-09 17:25:00 +0200326 to its class name. If you do, and there is no ``__construct()``
327 method in the same class, then your e.g. ``Index::index()``
328 method will be executed as a class constructor! This is a PHP4
Andrey Andreev522c7362012-11-05 16:40:32 +0200329 backwards-compatibility feature.
330
Derek Jones8ede1a22011-10-05 13:34:52 -0500331That's it!
332==========
333
Andrey Andreev522c7362012-11-05 16:40:32 +0200334That, in a nutshell, is all there is to know about controllers.