Skip to content

Commit 1e8dfe0

Browse files
committed
docs: add sample code with variable-length argument lists
For multiple URI segments matched a placeholder.
1 parent a5ee50c commit 1e8dfe0

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
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

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)