blob: 150d2269c27ca459aadcb84af245e6272b708ad3 [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 }
41 ?>
42
Derek Jones8ede1a22011-10-05 13:34:52 -050043Then save the file to your application/controllers/ folder.
44
45Now visit the your site using a URL similar to this::
46
47 example.com/index.php/blog/
48
49If you did it right, you should see Hello World!.
50
51Note: Class names must start with an uppercase letter. In other words,
52this is valid::
53
Derek Jonese69b4562011-10-05 17:30:50 -050054 <?php
55 class Blog extends CI_Controller {
56
57 }
58 ?>
59
Derek Jones8ede1a22011-10-05 13:34:52 -050060
61This is **not** valid::
62
Derek Jonese69b4562011-10-05 17:30:50 -050063 <?php
64 class blog extends CI_Controller {
65
66 }
67 ?>
Derek Jones8ede1a22011-10-05 13:34:52 -050068
69Also, always make sure your controller extends the parent controller
Andrey Andreev522c7362012-11-05 16:40:32 +020070class so that it can inherit all its methods.
Derek Jones8ede1a22011-10-05 13:34:52 -050071
Andrey Andreev522c7362012-11-05 16:40:32 +020072Methods
73=======
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Andrey Andreev522c7362012-11-05 16:40:32 +020075In the above example the method name is ``index()``. The "index" method
Derek Jones8ede1a22011-10-05 13:34:52 -050076is always loaded by default if the **second segment** of the URI is
77empty. Another way to show your "Hello World" message would be this::
78
79 example.com/index.php/blog/index/
80
Andrey Andreev522c7362012-11-05 16:40:32 +020081**The second segment of the URI determines which method in the
Derek Jones8ede1a22011-10-05 13:34:52 -050082controller gets called.**
83
Andrey Andreev522c7362012-11-05 16:40:32 +020084Let's try it. Add a new method to your controller::
Derek Jones8ede1a22011-10-05 13:34:52 -050085
Derek Jonese69b4562011-10-05 17:30:50 -050086 <?php
87 class Blog extends CI_Controller {
88
89 public function index()
90 {
91 echo 'Hello World!';
92 }
93
94 public function comments()
95 {
96 echo 'Look at this!';
97 }
98 }
99 ?>
100
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
110If your URI contains more then 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
113For example, lets say you have a URI like this::
114
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 }
128 ?>
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
130.. important:: If you are using the :doc:`URI Routing <routing>`
Andrey Andreev522c7362012-11-05 16:40:32 +0200131 feature, the segments passed to your method will be the re-routed
Derek Jones8ede1a22011-10-05 13:34:52 -0500132 ones.
133
134Defining a Default Controller
135=============================
136
137CodeIgniter can be told to load a default controller when a URI is not
138present, as will be the case when only your site root URL is requested.
purwandi02df61f2011-10-07 15:33:40 +0700139To specify a default controller, open your **application/config/routes.php**
Derek Jones8ede1a22011-10-05 13:34:52 -0500140file and set this variable::
141
142 $route['default_controller'] = 'Blog';
143
144Where Blog is the name of the controller class you want used. If you now
145load your main index.php file without specifying any URI segments you'll
146see your Hello World message by default.
147
Andrey Andreev522c7362012-11-05 16:40:32 +0200148Remapping Method Calls
149======================
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
151As noted above, the second segment of the URI typically determines which
Andrey Andreev522c7362012-11-05 16:40:32 +0200152method in the controller gets called. CodeIgniter permits you to override
153this behavior through the use of the ``_remap()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
Derek Jonese69b4562011-10-05 17:30:50 -0500155 public function _remap()
156 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200157 // Some code here...
Derek Jonese69b4562011-10-05 17:30:50 -0500158 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
Andrey Andreev522c7362012-11-05 16:40:32 +0200160.. important:: If your controller contains a method named _remap(),
Derek Jones8ede1a22011-10-05 13:34:52 -0500161 it will **always** get called regardless of what your URI contains. It
Andrey Andreev522c7362012-11-05 16:40:32 +0200162 overrides the normal behavior in which the URI determines which method
163 is called, allowing you to define your own method routing rules.
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreev522c7362012-11-05 16:40:32 +0200165The overridden method call (typically the second segment of the URI) will
166be passed as a parameter to the ``_remap()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Derek Jonese69b4562011-10-05 17:30:50 -0500168 public function _remap($method)
169 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200170 if ($method == 'some_method')
171 {
172 $this->$method();
173 }
174 else
175 {
176 $this->default_method();
177 }
Derek Jonese69b4562011-10-05 17:30:50 -0500178 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Andrey Andreev522c7362012-11-05 16:40:32 +0200180Any extra segments after the method name are passed into ``_remap()`` as an
Derek Jones8ede1a22011-10-05 13:34:52 -0500181optional second parameter. This array can be used in combination with
Andrey Andreev522c7362012-11-05 16:40:32 +0200182PHP's `call_user_func_array() <http://php.net/call_user_func_array>`_
Derek Jones8ede1a22011-10-05 13:34:52 -0500183to emulate CodeIgniter's default behavior.
184
185::
186
Derek Jonese69b4562011-10-05 17:30:50 -0500187 public function _remap($method, $params = array())
188 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200189 $method = 'process_'.$method;
190 if (method_exists($this, $method))
191 {
192 return call_user_func_array(array($this, $method), $params);
193 }
194 show_404();
Derek Jonese69b4562011-10-05 17:30:50 -0500195 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500196
197Processing Output
198=================
199
200CodeIgniter has an output class that takes care of sending your final
201rendered data to the web browser automatically. More information on this
Andrey Andreev522c7362012-11-05 16:40:32 +0200202can be found in the :doc:`Views <views>` and :doc:`Output Class
203<../libraries/output>` pages. In some cases, however, you might want to
204post-process the finalized data in some way and send it to the browser
205yourself. CodeIgniter permits you to add a method named ``_output()``
206to your controller that will receive the finalized output data.
Derek Jones8ede1a22011-10-05 13:34:52 -0500207
Andrey Andreev522c7362012-11-05 16:40:32 +0200208.. important:: If your controller contains a method named _output(), it
209 will **always** be called by the output class instead of echoing
210 the finalized data directly. The first parameter of the method
211 will contain the finalized output.
Derek Jones8ede1a22011-10-05 13:34:52 -0500212
213Here is an example::
214
Derek Jonese69b4562011-10-05 17:30:50 -0500215 public function _output($output)
216 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200217 echo $output;
Derek Jonese69b4562011-10-05 17:30:50 -0500218 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500219
Andrey Andreev522c7362012-11-05 16:40:32 +0200220.. note:: Please note that your _output() method will receive the data in its
Derek Jonese69b4562011-10-05 17:30:50 -0500221 finalized state. Benchmark and memory usage data will be rendered, cache
222 files written (if you have caching enabled), and headers will be sent
223 (if you use that :doc:`feature <../libraries/output>`) before it is
Andrey Andreev522c7362012-11-05 16:40:32 +0200224 handed off to the _output() method.
Derek Jonese69b4562011-10-05 17:30:50 -0500225 To have your controller's output cached properly, its _output() method
226 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 {
230 $this->output->_write_cache($output);
231 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500232
Derek Jonese69b4562011-10-05 17:30:50 -0500233 If you are using this feature the page execution timer and memory usage
234 stats might not be perfectly accurate since they will not take into
235 acccount any further processing you do. For an alternate way to control
236 output *before* any of the final processing is done, please see the
237 available methods in the :doc:`Output Class <../libraries/output>`.
Derek Jones8ede1a22011-10-05 13:34:52 -0500238
Andrey Andreev522c7362012-11-05 16:40:32 +0200239Private methods
240===============
Derek Jones8ede1a22011-10-05 13:34:52 -0500241
Andrey Andreev522c7362012-11-05 16:40:32 +0200242In some cases you may want certain methods hidden from public access.
243In order to achieve this, simply declare the method as being private
244or protected and it will not be served via a URL request. For example,
245if you were to have a method like this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500246
Derek Jonese69b4562011-10-05 17:30:50 -0500247 private function _utility()
248 {
Andrey Andreev522c7362012-11-05 16:40:32 +0200249 // some code
Derek Jonese69b4562011-10-05 17:30:50 -0500250 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500251
252Trying to access it via the URL, like this, will not work::
253
254 example.com/index.php/blog/_utility/
255
Andrey Andreev522c7362012-11-05 16:40:32 +0200256.. note:: Prefixing method names with an underscore will also prevent
257 them from being called. This is a legacy feature that is left
258 for backwards-compatibility.
259
Derek Jones8ede1a22011-10-05 13:34:52 -0500260Organizing Your Controllers into Sub-folders
261============================================
262
263If you are building a large application you might find it convenient to
264organize your controllers into sub-folders. CodeIgniter permits you to
265do this.
266
267Simply create folders within your application/controllers directory and
268place your controller classes within them.
269
270.. note:: When using this feature the first segment of your URI must
271 specify the folder. For example, lets say you have a controller located
272 here::
273
274 application/controllers/products/shoes.php
275
276 To call the above controller your URI will look something like this::
277
278 example.com/index.php/products/shoes/show/123
279
280Each of your sub-folders may contain a default controller which will be
281called if the URL contains only the sub-folder. Simply name your default
282controller as specified in your application/config/routes.php file
283
284CodeIgniter also permits you to remap your URIs using its :doc:`URI
285Routing <routing>` feature.
286
287Class Constructors
288==================
289
290If you intend to use a constructor in any of your Controllers, you
291**MUST** place the following line of code in it::
292
293 parent::__construct();
294
295The reason this line is necessary is because your local constructor will
296be overriding the one in the parent controller class so we need to
297manually call it.
298
299::
300
Derek Jonese69b4562011-10-05 17:30:50 -0500301 <?php
302 class Blog extends CI_Controller {
303
Andrey Andreev522c7362012-11-05 16:40:32 +0200304 public function __construct()
305 {
306 parent::__construct();
307 // Your own constructor code
308 }
Derek Jonese69b4562011-10-05 17:30:50 -0500309 }
310 ?>
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
326 to its class name. If you do, and there is no __construct()
327 method in the same class, then your e.g. Index::index() method
328 will be executed as a class constructor! This is a PHP4
329 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.