Skip to content

Commit 99e3d4a

Browse files
authored
Merge pull request #8340 from kenjis/docs-improve-routing
docs: improve routing
2 parents f69a39b + 1e8dfe0 commit 99e3d4a

File tree

9 files changed

+40
-4
lines changed

9 files changed

+40
-4
lines changed

user_guide_src/source/incoming/routing.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,19 @@ For example the route:
200200

201201
will match **product/123**, **product/123/456**, **product/123/456/789** and so on.
202202

203+
In the above example, if the ``$1`` placeholder contains a slash
204+
(``/``), it will still be split into multiple parameters when passed to
205+
``Catalog::productLookup()``.
206+
203207
The implementation in the
204208
Controller should take into account the maximum parameters:
205209

206210
.. literalinclude:: routing/011.php
207211

212+
Or you can use `variable-length argument lists <https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list>`_:
213+
214+
.. literalinclude:: routing/068.php
215+
208216
.. important:: Do not put any placeholder after ``(:any)``. Because the number of
209217
parameters passed to the controller method may change.
210218

@@ -249,6 +257,10 @@ redirect them back to the same page after they log in, you may find this example
249257

250258
.. literalinclude:: routing/019.php
251259

260+
In the above example, if the ``$1`` placeholder contains a slash
261+
(``/``), it will still be split into multiple parameters when passed to
262+
``Auth::login()``.
263+
252264
For those of you who don't know regular expressions and want to learn more about them,
253265
`regular-expressions.info <https://www.regular-expressions.info/>`_ might be a good starting point.
254266

user_guide_src/source/incoming/routing/011.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Controllers;
44

5-
class ProductController extends BaseController
5+
class Catalog extends BaseController
66
{
77
public function productLookup($seg1 = false, $seg2 = false, $seg3 = false)
88
{

user_guide_src/source/incoming/routing/045.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Routing extends BaseRouting
88
// ...
99
}
1010

11+
// In app/Config/Routes.php
1112
// Controller is \Users
1213
$routes->get('users', 'Users::index');
1314

user_guide_src/source/incoming/routing/046.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
// This can be overridden in the Routes file
3+
// This can be overridden in app/Config/Routes.php
44
$routes->setDefaultNamespace('App');
55

66
// Controller is \App\Users

user_guide_src/source/incoming/routing/049.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ class Routing extends BaseRouting
88
// ...
99
}
1010

11-
// This can be overridden in the Routes file
11+
// This can be overridden in app/Config/Routes.php
1212
$routes->setTranslateURIDashes(true);

user_guide_src/source/incoming/routing/050.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ class Routing extends BaseRouting
88
// ...
99
}
1010

11-
// This can be overridden in the Routes file
11+
// This can be overridden in app/Config/Routes.php
1212
$routes->setAutoRoute(false);

user_guide_src/source/incoming/routing/051.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Routing extends BaseRouting
88
// ...
99
}
1010

11+
// In app/Config/Routes.php
1112
// Would execute the show404 method of the App\Errors class
1213
$routes->set404Override('App\Errors::show404');
1314

user_guide_src/source/incoming/routing/052.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
// In app/Config/Routing.php
4+
class Routing extends BaseRouting
5+
{
6+
// ...
7+
public bool $prioritize = true;
8+
// ...
9+
}
10+
11+
// In app/Config/Routes.php
312
// to enable
413
$routes->setPrioritize();
514

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
class Catalog extends BaseController
6+
{
7+
public function productLookup(...$params)
8+
{
9+
echo $params[0] ?? null; // Will be 123 in all examples
10+
echo $params[1] ?? null; // null in first, 456 in second and third example
11+
echo $params[2] ?? null; // null in first and second, 789 in third
12+
}
13+
}

0 commit comments

Comments
 (0)