Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########### |
| 2 | Controllers |
| 3 | ########### |
| 4 | |
| 5 | Controllers are the heart of your application, as they determine how |
| 6 | HTTP requests should be handled. |
| 7 | |
Joseph Wensley | 5b3ea1a | 2011-10-06 20:54:32 -0400 | [diff] [blame] | 8 | .. contents:: Page Contents |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 9 | |
| 10 | What is a Controller? |
| 11 | ===================== |
| 12 | |
purwandi | 02df61f | 2011-10-07 15:33:40 +0700 | [diff] [blame] | 13 | **A Controller is simply a class file that is named in a way that can be |
| 14 | associated with a URI.** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 15 | |
| 16 | Consider this URI:: |
| 17 | |
| 18 | example.com/index.php/blog/ |
| 19 | |
| 20 | In the above example, CodeIgniter would attempt to find a controller |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 21 | named Blog.php and load it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 22 | |
| 23 | **When a controller's name matches the first segment of a URI, it will |
| 24 | be loaded.** |
| 25 | |
| 26 | Let's try it: Hello World! |
| 27 | ========================== |
| 28 | |
| 29 | Let's create a simple controller so you can see it in action. Using your |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 30 | text editor, create a file called Blog.php, and put the following code |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 31 | in it:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 32 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 33 | <?php |
| 34 | class Blog extends CI_Controller { |
| 35 | |
| 36 | public function index() |
| 37 | { |
| 38 | echo 'Hello World!'; |
| 39 | } |
| 40 | } |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 41 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 42 | Then save the file to your *application/controllers/* directory. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 44 | .. important:: The file must be called 'Blog.php', with a capital 'B'. |
| 45 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | Now visit the your site using a URL similar to this:: |
| 47 | |
| 48 | example.com/index.php/blog/ |
| 49 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 50 | If you did it right, you should see: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 52 | Hello World! |
| 53 | |
| 54 | .. important:: Class names must start with an uppercase letter. |
| 55 | |
| 56 | This is valid:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 57 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 58 | <?php |
| 59 | class Blog extends CI_Controller { |
| 60 | |
| 61 | } |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 62 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 63 | This is **not** valid:: |
| 64 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 65 | <?php |
| 66 | class blog extends CI_Controller { |
| 67 | |
| 68 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 69 | |
| 70 | Also, always make sure your controller extends the parent controller |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 71 | class so that it can inherit all its methods. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 73 | Methods |
| 74 | ======= |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 76 | In the above example the method name is ``index()``. The "index" method |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | is always loaded by default if the **second segment** of the URI is |
| 78 | empty. Another way to show your "Hello World" message would be this:: |
| 79 | |
| 80 | example.com/index.php/blog/index/ |
| 81 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 82 | **The second segment of the URI determines which method in the |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | controller gets called.** |
| 84 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 85 | Let's try it. Add a new method to your controller:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 87 | <?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 Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 100 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 101 | Now load the following URL to see the comment method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 102 | |
| 103 | example.com/index.php/blog/comments/ |
| 104 | |
| 105 | You should see your new message. |
| 106 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 107 | Passing URI Segments to your methods |
| 108 | ==================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 109 | |
Andrey Andreev | ba231aa | 2014-01-20 16:43:41 +0200 | [diff] [blame] | 110 | If your URI contains more than two segments they will be passed to your |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 111 | method as parameters. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
vlakoff | 3567246 | 2013-02-15 01:36:04 +0100 | [diff] [blame] | 113 | For example, let's say you have a URI like this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
| 115 | example.com/index.php/products/shoes/sandals/123 |
| 116 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 117 | Your method will be passed URI segments 3 and 4 ("sandals" and "123"):: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 119 | <?php |
| 120 | class Products extends CI_Controller { |
| 121 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 122 | public function shoes($sandals, $id) |
| 123 | { |
| 124 | echo $sandals; |
| 125 | echo $id; |
| 126 | } |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 127 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | |
| 129 | .. important:: If you are using the :doc:`URI Routing <routing>` |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 130 | feature, the segments passed to your method will be the re-routed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | ones. |
| 132 | |
| 133 | Defining a Default Controller |
| 134 | ============================= |
| 135 | |
| 136 | CodeIgniter can be told to load a default controller when a URI is not |
| 137 | present, as will be the case when only your site root URL is requested. |
purwandi | 02df61f | 2011-10-07 15:33:40 +0700 | [diff] [blame] | 138 | To specify a default controller, open your **application/config/routes.php** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | file and set this variable:: |
| 140 | |
Master Yoda | 12617cf | 2015-04-30 02:32:59 -0700 | [diff] [blame] | 141 | $route['default_controller'] = 'blog'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 142 | |
Andrey Andreev | ee2ece8 | 2015-09-28 13:18:05 +0300 | [diff] [blame] | 143 | Where 'blog' is the name of the controller class you want used. If you now |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 144 | load your main index.php file without specifying any URI segments you'll |
Andrey Andreev | ee2ece8 | 2015-09-28 13:18:05 +0300 | [diff] [blame] | 145 | see your "Hello World" message by default. |
| 146 | |
| 147 | For more information, please refer to the "Reserved Routes" section of the |
Andrey Andreev | c094bec | 2015-10-08 17:18:57 +0300 | [diff] [blame] | 148 | :doc:`URI Routing <routing>` documentation. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 149 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 150 | Remapping Method Calls |
| 151 | ====================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
| 153 | As noted above, the second segment of the URI typically determines which |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 154 | method in the controller gets called. CodeIgniter permits you to override |
| 155 | this behavior through the use of the ``_remap()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 156 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 157 | public function _remap() |
| 158 | { |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 159 | // Some code here... |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 160 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 161 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 162 | .. important:: If your controller contains a method named _remap(), |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | it will **always** get called regardless of what your URI contains. It |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 164 | overrides the normal behavior in which the URI determines which method |
| 165 | is called, allowing you to define your own method routing rules. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 166 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 167 | The overridden method call (typically the second segment of the URI) will |
| 168 | be passed as a parameter to the ``_remap()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 169 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 170 | public function _remap($method) |
| 171 | { |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 172 | if ($method === 'some_method') |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 173 | { |
| 174 | $this->$method(); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | $this->default_method(); |
| 179 | } |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 180 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 181 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 182 | Any extra segments after the method name are passed into ``_remap()`` as an |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 183 | optional second parameter. This array can be used in combination with |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 184 | PHP's `call_user_func_array() <http://php.net/call_user_func_array>`_ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 185 | to emulate CodeIgniter's default behavior. |
| 186 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 187 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 188 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 189 | public function _remap($method, $params = array()) |
| 190 | { |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 191 | $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 Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 197 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | |
| 199 | Processing Output |
| 200 | ================= |
| 201 | |
| 202 | CodeIgniter has an output class that takes care of sending your final |
| 203 | rendered data to the web browser automatically. More information on this |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 204 | can be found in the :doc:`Views <views>` and :doc:`Output Class |
| 205 | <../libraries/output>` pages. In some cases, however, you might want to |
| 206 | post-process the finalized data in some way and send it to the browser |
| 207 | yourself. CodeIgniter permits you to add a method named ``_output()`` |
| 208 | to your controller that will receive the finalized output data. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 209 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 210 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 214 | |
| 215 | Here is an example:: |
| 216 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 217 | public function _output($output) |
| 218 | { |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 219 | echo $output; |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 220 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 221 | |
Kevin Smith | d3f9efe | 2013-05-13 16:34:37 -0500 | [diff] [blame] | 222 | .. note:: |
| 223 | |
| 224 | Please note that your ``_output()`` method will receive the |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 225 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 232 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 233 | if ($this->output->cache_expiration > 0) |
| 234 | { |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 235 | $this->output->_write_cache($output); |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 236 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 237 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 238 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 244 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 245 | Private methods |
| 246 | =============== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 247 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 248 | In some cases you may want certain methods hidden from public access. |
| 249 | In order to achieve this, simply declare the method as being private |
| 250 | or protected and it will not be served via a URL request. For example, |
| 251 | if you were to have a method like this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 252 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 253 | private function _utility() |
| 254 | { |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 255 | // some code |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 256 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 257 | |
| 258 | Trying to access it via the URL, like this, will not work:: |
| 259 | |
| 260 | example.com/index.php/blog/_utility/ |
| 261 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 262 | .. 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 Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 266 | Organizing Your Controllers into Sub-directories |
| 267 | ================================================ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 268 | |
Andrey Andreev | ee2ece8 | 2015-09-28 13:18:05 +0300 | [diff] [blame] | 269 | If you are building a large application you might want to hierarchically |
| 270 | organize or structure your controllers into sub-directories. CodeIgniter |
| 271 | permits you to do this. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 272 | |
Andrey Andreev | ee2ece8 | 2015-09-28 13:18:05 +0300 | [diff] [blame] | 273 | Simply create sub-directories under the main *application/controllers/* |
| 274 | one and place your controller classes within them. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 275 | |
| 276 | .. note:: When using this feature the first segment of your URI must |
vlakoff | 3567246 | 2013-02-15 01:36:04 +0100 | [diff] [blame] | 277 | specify the folder. For example, let's say you have a controller located |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 278 | here:: |
| 279 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 280 | application/controllers/products/Shoes.php |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 281 | |
| 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 Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 286 | Each of your sub-directories may contain a default controller which will be |
Andrey Andreev | ee2ece8 | 2015-09-28 13:18:05 +0300 | [diff] [blame] | 287 | called if the URL contains *only* the sub-directory. Simply put a controller |
| 288 | in there that matches the name of your 'default_controller' as specified in |
| 289 | your *application/config/routes.php* file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 290 | |
| 291 | CodeIgniter also permits you to remap your URIs using its :doc:`URI |
| 292 | Routing <routing>` feature. |
| 293 | |
| 294 | Class Constructors |
| 295 | ================== |
| 296 | |
| 297 | If 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 | |
| 302 | The reason this line is necessary is because your local constructor will |
| 303 | be overriding the one in the parent controller class so we need to |
| 304 | manually call it. |
| 305 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 306 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 307 | |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 308 | <?php |
| 309 | class Blog extends CI_Controller { |
| 310 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 311 | public function __construct() |
| 312 | { |
| 313 | parent::__construct(); |
| 314 | // Your own constructor code |
| 315 | } |
Derek Jones | e69b456 | 2011-10-05 17:30:50 -0500 | [diff] [blame] | 316 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 317 | |
| 318 | Constructors are useful if you need to set some default values, or run a |
| 319 | default process when your class is instantiated. Constructors can't |
| 320 | return a value, but they can do some default work. |
| 321 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 322 | Reserved method names |
| 323 | ===================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 324 | |
| 325 | Since your controller classes will extend the main application |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 326 | controller you must be careful not to name your methods identically to |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 327 | the ones used by that class, otherwise your local functions will |
| 328 | override them. See :doc:`Reserved Names <reserved_names>` for a full |
| 329 | list. |
| 330 | |
Andrey Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 331 | .. important:: You should also never have a method named identically |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 332 | 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 Andreev | 522c736 | 2012-11-05 16:40:32 +0200 | [diff] [blame] | 335 | backwards-compatibility feature. |
| 336 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 337 | That's it! |
| 338 | ========== |
| 339 | |
Andrey Andreev | 4a56778 | 2017-11-20 09:58:03 +0200 | [diff] [blame^] | 340 | That, in a nutshell, is all there is to know about controllers. |