Skip to content

Commit 987f3da

Browse files
authored
Merge pull request #7414 from kenjis/docs-auto-routing
docs: improve auto routing
2 parents 3733dab + 095d063 commit 987f3da

2 files changed

Lines changed: 70 additions & 38 deletions

File tree

user_guide_src/source/incoming/controllers.rst

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ controllers. You can extend this class in any new controller.
187187

188188
.. literalinclude:: controllers/020.php
189189

190-
Then save the file to your **app/Controllers/** directory.
190+
Then save the file to your **app/Controllers** directory.
191191

192192
.. 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.
193193

@@ -218,8 +218,8 @@ class so that it can inherit all its methods.
218218

219219
.. note::
220220
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.
223223

224224
If you want another naming convention you need to manually define it using the
225225
:ref:`Defined Route Routing <defined-route-routing>`.
@@ -274,16 +274,21 @@ Your method will be passed URI segments 3 and 4 (``'sandals'`` and ``'123'``):
274274

275275
.. literalinclude:: controllers/022.php
276276

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,
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 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+
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
================================================
@@ -311,21 +319,22 @@ If you are building a large application you might want to hierarchically
311319
organize or structure your controllers into sub-directories. CodeIgniter
312320
permits you to do this.
313321

314-
Simply create sub-directories under the main **app/Controllers/**,
322+
Simply create sub-directories under the main **app/Controllers**,
315323
and place your controller classes within them.
316324

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

319327
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::
321329

322330
app/Controllers/Products/Shoes.php
323331

324332
To call the above controller your URI will look something like this::
325333

326334
example.com/index.php/products/shoes/show/123
327335

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**.
329338
This is because if there is a directory, the web server will search for it and
330339
it will not be routed to CodeIgniter.
331340

@@ -359,8 +368,8 @@ In the above example, CodeIgniter would attempt to find a controller named **Hel
359368

360369
.. note:: When a controller's short name matches the first segment of a URI, it will be loaded.
361370

362-
Let's try it: Hello World!
363-
==========================
371+
Let's try it: Hello World! (Legacy)
372+
===================================
364373

365374
Let's create a simple controller so you can see it in action. Using your text editor, create a file called **Helloworld.php**,
366375
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``
373382

374383
.. literalinclude:: controllers/008.php
375384

376-
Then save the file to your **app/Controllers/** directory.
385+
Then save the file to your **app/Controllers** directory.
377386

378387
.. 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.
379388

@@ -402,17 +411,17 @@ class so that it can inherit all its methods.
402411

403412
.. note::
404413
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.
407416

408417
If you want another naming convention you need to manually define it using the
409418
:ref:`Defined Route Routing <defined-route-routing>`.
410419
Here is an example based on PSR-4 Autoloader:
411420

412421
.. literalinclude:: controllers/012.php
413422

414-
Methods
415-
=======
423+
Methods (Legacy)
424+
================
416425

417426
In the above example, the method name is ``index()``. The ``index()`` method
418427
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::
433442

434443
You should see your new message.
435444

436-
Passing URI Segments to Your Methods
437-
====================================
445+
Passing URI Segments to Your Methods (Legacy)
446+
=============================================
438447

439448
If your URI contains more than two segments they will be passed to your
440449
method as parameters.
@@ -447,12 +456,17 @@ Your method will be passed URI segments 3 and 4 (``'sandals'`` and ``'123'``):
447456

448457
.. literalinclude:: controllers/014.php
449458

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

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

457471
To specify a default controller open your **app/Config/Routes.php**
458472
file and set this variable:
@@ -471,30 +485,30 @@ see the "Hello World" message.
471485
.. 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>`
472486

473487
For more information, please refer to the :ref:`routes-configuration-options` section of the
474-
:doc:`URI Routing <routing>` documentation.
488+
:ref:`URI Routing <routing-auto-routing-legacy-configuration-options>` documentation.
475489

476-
Organizing Your Controllers into Sub-directories
477-
================================================
490+
Organizing Your Controllers into Sub-directories (Legacy)
491+
=========================================================
478492

479493
If you are building a large application you might want to hierarchically
480494
organize or structure your controllers into sub-directories. CodeIgniter
481495
permits you to do this.
482496

483-
Simply create sub-directories under the main **app/Controllers/**,
497+
Simply create sub-directories under the main **app/Controllers**,
484498
and place your controller classes within them.
485499

486-
.. important:: Folder names MUST start with an uppercase letter and ONLY the first character can be uppercase.
500+
.. important:: Directory names MUST start with an uppercase letter and ONLY the first character can be uppercase.
487501

488502
When using this feature the first segment of your URI must
489-
specify the folder. For example, let's say you have a controller located here::
503+
specify the directory. For example, let's say you have a controller located here::
490504

491505
app/Controllers/Products/Shoes.php
492506

493507
To call the above controller your URI will look something like this::
494508

495509
example.com/index.php/products/shoes/show/123
496510

497-
.. note:: You cannot have directories with the same name in **app/Controllers/** and **public/**.
511+
.. note:: You cannot have directories with the same name in **app/Controllers** and **public/**.
498512
This is because if there is a directory, the web server will search for it and
499513
it will not be routed to CodeIgniter.
500514

user_guide_src/source/incoming/routing.rst

Lines changed: 21 additions & 3 deletions
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,17 +673,25 @@ 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.
683693

684-
.. note:: You cannot access the default controller with the URI of the controller name.
694+
.. important:: You cannot access the default controller with the URI of the controller name.
685695
When the default controller is ``Home``, you can access **example.com/**, but if you access **example.com/home**, it will be not found.
686696

687697
See :ref:`Auto Routing in Controllers <controller-auto-routing-improved>` for more info.
@@ -698,7 +708,7 @@ In this example, if the user were to visit **example.com/products**, and a ``Pro
698708

699709
.. literalinclude:: routing/048.php
700710

701-
.. note:: You cannot access the controller with the URI of the default method name.
711+
.. important:: You cannot access the controller with the URI of the default method name.
702712
In the example above, you can access **example.com/products**, but if you access **example.com/products/listall**, it will be not found.
703713

704714
.. _auto-routing-legacy:
@@ -747,6 +757,8 @@ and executes ``index()`` method with passing ``'1'`` as the first argument.
747757

748758
See :ref:`Auto Routing (Legacy) in Controllers <controller-auto-routing-legacy>` for more info.
749759

760+
.. _routing-auto-routing-legacy-configuration-options:
761+
750762
Configuration Options (Legacy)
751763
==============================
752764

@@ -755,12 +767,18 @@ These options are available at the top of **app/Config/Routes.php**.
755767
Default Controller (Legacy)
756768
---------------------------
757769

770+
For Site Root URI (Legacy)
771+
^^^^^^^^^^^^^^^^^^^^^^^^^^
772+
758773
When a user visits the root of your site (i.e., example.com) the controller to use is determined by the value set by
759774
the ``setDefaultController()`` method, unless a route exists for it explicitly. The default value for this is ``Home``
760775
which matches the controller at **app/Controllers/Home.php**:
761776

762777
.. literalinclude:: routing/047.php
763778

779+
For Directory URI (Legacy)
780+
^^^^^^^^^^^^^^^^^^^^^^^^^^
781+
764782
The default controller is also used when no matching route has been found, and the URI would point to a directory
765783
in the controllers directory. For example, if the user visits **example.com/admin**, if a controller was found at
766784
**app/Controllers/Admin/Home.php**, it would be used.

0 commit comments

Comments
 (0)