Skip to content

Commit 0879913

Browse files
committed
docs: improve description for Default Controller in Auto-Routing Improved
1 parent ac1d6dc commit 0879913

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

user_guide_src/source/incoming/controllers.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,17 @@ Your method will be passed URI segments 3 and 4 (``'sandals'`` and ``'123'``):
278278
Auto Routing (Improved) does not execute the method, and it results in 404
279279
Not Found.
280280

281+
Default Controller
282+
==================
283+
284+
The Default Controller is a special controller that is used when a URI end 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+
281288
Defining a Default Controller
282-
=============================
289+
-----------------------------
283290

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.
287292

288293
To specify a default controller open your **app/Config/Routes.php**
289294
file and set this variable:
@@ -299,10 +304,13 @@ A few lines further down **Routes.php** in the "Route Definitions" section, comm
299304
If you now browse to your site without specifying any URI segments you'll
300305
see the "Hello World" message.
301306

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.
303311

304312
For more information, please refer to the :ref:`routes-configuration-options` section of the
305-
:doc:`URI Routing <routing>` documentation.
313+
:ref:`URI Routing <routing-auto-routing-improved-configuration-options>` documentation.
306314

307315
Organizing Your Controllers into Sub-directories
308316
================================================

user_guide_src/source/incoming/routing.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,8 @@ and executes ``getHello()`` method with passing ``'1'`` as the first argument.
663663

664664
See :ref:`Auto Routing in Controllers <controller-auto-routing-improved>` for more info.
665665

666+
.. _routing-auto-routing-improved-configuration-options:
667+
666668
Configuration Options
667669
=====================
668670

@@ -671,12 +673,20 @@ These options are available at the top of **app/Config/Routes.php**.
671673
Default Controller
672674
------------------
673675

676+
For Site Root URI
677+
^^^^^^^^^^^^^^^^^
678+
674679
When a user visits the root of your site (i.e., **example.com**) the controller to use is determined by the value set by
675-
the ``setDefaultController()`` method, unless a route exists for it explicitly. The default value for this is ``Home``
680+
the ``setDefaultController()`` method, unless a route exists for it explicitly.
681+
682+
The default value for this is ``Home``
676683
which matches the controller at **app/Controllers/Home.php**:
677684

678685
.. literalinclude:: routing/047.php
679686

687+
For Directory URI
688+
^^^^^^^^^^^^^^^^^
689+
680690
The default controller is also used when no matching route has been found, and the URI would point to a directory
681691
in the controllers directory. For example, if the user visits **example.com/admin**, if a controller was found at
682692
**app/Controllers/Admin/Home.php**, it would be used.

0 commit comments

Comments
 (0)