Skip to content

Commit 305ce77

Browse files
authored
Merge pull request #6960 from kenjis/fix-docs-routing.rst
docs: improve routing.rst
2 parents 0fbc883 + 340e2a7 commit 305ce77

1 file changed

Lines changed: 36 additions & 29 deletions

File tree

user_guide_src/source/incoming/routing.rst

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,42 @@ If you expect a GET request, you use the ``get()`` method:
3232
.. literalinclude:: routing/001.php
3333

3434
A route takes the URI path (``/``) on the left, and maps it to the controller and method (``Home::index``) on the right,
35-
along with any parameters that should be passed to the controller. The controller and method should
35+
along with any parameters that should be passed to the controller.
36+
37+
The controller and method should
3638
be listed in the same way that you would use a static method, by separating the class
37-
and its method with a double-colon, like ``Users::list``. If that method requires parameters to be
39+
and its method with a double-colon, like ``Users::list``.
40+
41+
If that method requires parameters to be
3842
passed to it, then they would be listed after the method name, separated by forward-slashes:
3943

4044
.. literalinclude:: routing/002.php
4145

46+
Examples
47+
========
48+
49+
Here are a few basic routing examples.
50+
51+
A URL containing the word **journals** in the first segment will be mapped to the ``\App\Controllers\Blogs`` class,
52+
and the default method, which is usually ``index()``:
53+
54+
.. literalinclude:: routing/006.php
55+
56+
A URL containing the segments **blog/joe** will be mapped to the ``\App\Controllers\Blogs`` class and the ``users()`` method.
57+
The ID will be set to ``34``:
58+
59+
.. literalinclude:: routing/007.php
60+
61+
A URL with **product** as the first segment, and anything in the second will be mapped to the ``\App\Controllers\Catalog`` class
62+
and the ``productLookup()`` method:
63+
64+
.. literalinclude:: routing/008.php
65+
66+
A URL with **product** as the first segment, and a number in the second will be mapped to the ``\App\Controllers\Catalog`` class
67+
and the ``productLookupByID()`` method passing in the match as a variable to the method:
68+
69+
.. literalinclude:: routing/009.php
70+
4271
HTTP verbs
4372
==========
4473

@@ -99,31 +128,6 @@ Placeholders Description
99128
.. note:: ``{locale}`` cannot be used as a placeholder or other part of the route, as it is reserved for use
100129
in :doc:`localization </outgoing/localization>`.
101130

102-
Examples
103-
========
104-
105-
Here are a few basic routing examples.
106-
107-
A URL containing the word **journals** in the first segment will be mapped to the ``\App\Controllers\Blogs`` class,
108-
and the default method, which is usually ``index()``:
109-
110-
.. literalinclude:: routing/006.php
111-
112-
A URL containing the segments **blog/joe** will be mapped to the ``\App\Controllers\Blogs`` class and the ``users()`` method.
113-
The ID will be set to ``34``:
114-
115-
.. literalinclude:: routing/007.php
116-
117-
A URL with **product** as the first segment, and anything in the second will be mapped to the ``\App\Controllers\Catalog`` class
118-
and the ``productLookup()`` method:
119-
120-
.. literalinclude:: routing/008.php
121-
122-
A URL with **product** as the first segment, and a number in the second will be mapped to the ``\App\Controllers\Catalog`` class
123-
and the ``productLookupByID()`` method passing in the match as a variable to the method:
124-
125-
.. literalinclude:: routing/009.php
126-
127131
Note that a single ``(:any)`` will match multiple segments in the URL if present. For example the route:
128132

129133
.. literalinclude:: routing/010.php
@@ -133,6 +137,9 @@ Controller should take into account the maximum parameters:
133137

134138
.. literalinclude:: routing/011.php
135139

140+
.. important:: Do not put any placeholder after ``(:any)``. Because the number of
141+
parameters passed to the controller method may change.
142+
136143
If matching multiple segments is not the intended behavior, ``(:segment)`` should be used when defining the
137144
routes. With the examples URLs from above:
138145

@@ -188,7 +195,7 @@ is allowed, as are back-references.
188195

189196
.. literalinclude:: routing/018.php
190197

191-
In the above example, a URI similar to **products/shirts/123** would instead call the ``show`` method
198+
In the above example, a URI similar to **products/shirts/123** would instead call the ``show()`` method
192199
of the ``Products`` controller class, with the original first and second segment passed as arguments to it.
193200

194201
With regular expressions, you can also catch a segment containing a forward slash (``/``), which would usually
@@ -202,7 +209,7 @@ redirect them back to the same page after they log in, you may find this example
202209
For those of you who don't know regular expressions and want to learn more about them,
203210
`regular-expressions.info <https://www.regular-expressions.info/>`_ might be a good starting point.
204211

205-
.. important:: Note: You can also mix and match wildcards with regular expressions.
212+
.. note:: You can also mix and match placeholders with regular expressions.
206213

207214
Closures
208215
========

0 commit comments

Comments
 (0)