You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user_guide_src/source/incoming/controllers.rst
+49-35Lines changed: 49 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -187,7 +187,7 @@ controllers. You can extend this class in any new controller.
187
187
188
188
.. literalinclude:: controllers/020.php
189
189
190
-
Then save the file to your **app/Controllers/** directory.
190
+
Then save the file to your **app/Controllers** directory.
191
191
192
192
.. important:: The file must be called **Helloworld.php**, with a capital ``H``. When you use Auto Routing, Controller class names MUST start with an uppercase letter and ONLY the first character can be uppercase.
193
193
@@ -218,8 +218,8 @@ class so that it can inherit all its methods.
218
218
219
219
.. note::
220
220
The system will attempt to match the URI against Controllers by matching each segment against
221
-
folders/files in **app/Controllers/**, when a match wasn't found against defined routes.
222
-
That's why your folders/files MUST start with a capital letter and the rest MUST be lowercase.
221
+
directories/files in **app/Controllers**, when a match wasn't found against defined routes.
222
+
That's why your directories/files MUST start with a capital letter and the rest MUST be lowercase.
223
223
224
224
If you want another naming convention you need to manually define it using the
@@ -274,16 +274,21 @@ Your method will be passed URI segments 3 and 4 (``'sandals'`` and ``'123'``):
274
274
275
275
.. literalinclude:: controllers/022.php
276
276
277
-
.. note:: If there are more parameters in the URI than the method parameters,
277
+
.. important:: If there are more parameters in the URI than the method parameters,
278
278
Auto Routing (Improved) does not execute the method, and it results in 404
279
279
Not Found.
280
280
281
+
Default Controller
282
+
==================
283
+
284
+
The Default Controller is a special controller that is used when a URI ends with
285
+
a directory name or when a URI is not present, as will be the case when only your
286
+
site root URL is requested.
287
+
281
288
Defining a Default Controller
282
-
=============================
289
+
-----------------------------
283
290
284
-
CodeIgniter can be told to load a default controller when a URI is not
285
-
present, as will be the case when only your site root URL is requested. Let's try it
286
-
with the ``Helloworld`` controller.
291
+
Let's try it with the ``Helloworld`` controller.
287
292
288
293
To specify a default controller open your **app/Config/Routes.php**
289
294
file and set this variable:
@@ -299,10 +304,13 @@ A few lines further down **Routes.php** in the "Route Definitions" section, comm
299
304
If you now browse to your site without specifying any URI segments you'll
300
305
see the "Hello World" message.
301
306
302
-
.. note:: The line ``$routes->get('/', 'Home::index');`` is an optimization that you will want to use in a "real-world" app. But for demonstration purposes we don't want to use that feature. ``$routes->get()`` is explained in :doc:`URI Routing <routing>`
307
+
.. important:: When you use Auto Routing (Improved), you must remove the line
308
+
``$routes->get('/', 'Home::index');``. Because defined routes take
309
+
precedence over Auto Routing, and controllers defined in the defined routes
310
+
are denied access by Auto Routing (Improved) for security reasons.
303
311
304
312
For more information, please refer to the :ref:`routes-configuration-options` section of the
@@ -311,21 +319,22 @@ If you are building a large application you might want to hierarchically
311
319
organize or structure your controllers into sub-directories. CodeIgniter
312
320
permits you to do this.
313
321
314
-
Simply create sub-directories under the main **app/Controllers/**,
322
+
Simply create sub-directories under the main **app/Controllers**,
315
323
and place your controller classes within them.
316
324
317
-
.. important:: Folder names MUST start with an uppercase letter and ONLY the first character can be uppercase.
325
+
.. important:: Directory names MUST start with an uppercase letter and ONLY the first character can be uppercase.
318
326
319
327
When using this feature the first segment of your URI must
320
-
specify the folder. For example, let's say you have a controller located here::
328
+
specify the directory. For example, let's say you have a controller located here::
321
329
322
330
app/Controllers/Products/Shoes.php
323
331
324
332
To call the above controller your URI will look something like this::
325
333
326
334
example.com/index.php/products/shoes/show/123
327
335
328
-
.. note:: You cannot have directories with the same name in **app/Controllers/** and **public/**.
336
+
.. note:: You cannot have directories with the same name in **app/Controllers**
337
+
and **public**.
329
338
This is because if there is a directory, the web server will search for it and
330
339
it will not be routed to CodeIgniter.
331
340
@@ -359,8 +368,8 @@ In the above example, CodeIgniter would attempt to find a controller named **Hel
359
368
360
369
.. note:: When a controller's short name matches the first segment of a URI, it will be loaded.
361
370
362
-
Let's try it: Hello World!
363
-
==========================
371
+
Let's try it: Hello World! (Legacy)
372
+
===================================
364
373
365
374
Let's create a simple controller so you can see it in action. Using your text editor, create a file called **Helloworld.php**,
366
375
and put the following code in it. You will notice that the ``Helloworld`` Controller is extending the ``BaseController``. you can
@@ -373,7 +382,7 @@ For security reasons be sure to declare any new utility methods as ``protected``
373
382
374
383
.. literalinclude:: controllers/008.php
375
384
376
-
Then save the file to your **app/Controllers/** directory.
385
+
Then save the file to your **app/Controllers** directory.
377
386
378
387
.. important:: The file must be called **Helloworld.php**, with a capital ``H``. When you use Auto Routing, Controller class names MUST start with an uppercase letter and ONLY the first character can be uppercase.
379
388
@@ -402,17 +411,17 @@ class so that it can inherit all its methods.
402
411
403
412
.. note::
404
413
The system will attempt to match the URI against Controllers by matching each segment against
405
-
folders/files in **app/Controllers/**, when a match wasn't found against defined routes.
406
-
That's why your folders/files MUST start with a capital letter and the rest MUST be lowercase.
414
+
directories/files in **app/Controllers**, when a match wasn't found against defined routes.
415
+
That's why your directories/files MUST start with a capital letter and the rest MUST be lowercase.
407
416
408
417
If you want another naming convention you need to manually define it using the
In the above example, the method name is ``index()``. The ``index()`` method
418
427
is always loaded by default if the **second segment** of the URI is
@@ -433,8 +442,8 @@ Now load the following URL to see the comment method::
433
442
434
443
You should see your new message.
435
444
436
-
Passing URI Segments to Your Methods
437
-
====================================
445
+
Passing URI Segments to Your Methods (Legacy)
446
+
=============================================
438
447
439
448
If your URI contains more than two segments they will be passed to your
440
449
method as parameters.
@@ -447,12 +456,17 @@ Your method will be passed URI segments 3 and 4 (``'sandals'`` and ``'123'``):
447
456
448
457
.. literalinclude:: controllers/014.php
449
458
450
-
Defining a Default Controller
451
-
=============================
459
+
Default Controller (Legacy)
460
+
===========================
461
+
462
+
The Default Controller is a special controller that is used when a URI end with
463
+
a directory name or when a URI is not present, as will be the case when only your
464
+
site root URL is requested.
452
465
453
-
CodeIgniter can be told to load a default controller when a URI is not
454
-
present, as will be the case when only your site root URL is requested. Let's try it
455
-
with the ``Helloworld`` controller.
466
+
Defining a Default Controller (Legacy)
467
+
--------------------------------------
468
+
469
+
Let's try it with the ``Helloworld`` controller.
456
470
457
471
To specify a default controller open your **app/Config/Routes.php**
458
472
file and set this variable:
@@ -471,30 +485,30 @@ see the "Hello World" message.
471
485
.. note:: The line ``$routes->get('/', 'Home::index');`` is an optimization that you will want to use in a "real-world" app. But for demonstration purposes we don't want to use that feature. ``$routes->get()`` is explained in :doc:`URI Routing <routing>`
472
486
473
487
For more information, please refer to the :ref:`routes-configuration-options` section of the
0 commit comments