Skip to content

Commit

Permalink
docs: add sample code with variable-length argument lists
Browse files Browse the repository at this point in the history
For multiple URI segments matched a placeholder.
  • Loading branch information
kenjis committed Dec 17, 2023
1 parent a5ee50c commit ed3af11
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions user_guide_src/source/incoming/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,19 @@ For example the route:

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

By default, in the above example, if the ``$1`` placeholder contains a slash
(``/``), it will still be split into multiple parameters when passed to
``Catalog::productLookup()``.

The implementation in the
Controller should take into account the maximum parameters:

.. literalinclude:: routing/011.php

Or you can use `variable-length argument lists <https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list>`_:

.. literalinclude:: routing/068.php

.. important:: Do not put any placeholder after ``(:any)``. Because the number of
parameters passed to the controller method may change.

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

.. literalinclude:: routing/019.php

By default, in the above example, if the ``$1`` placeholder contains a slash
(``/``), it will still be split into multiple parameters when passed to
``Auth::login()``.

For those of you who don't know regular expressions and want to learn more about them,
`regular-expressions.info <https://www.regular-expressions.info/>`_ might be a good starting point.

Expand Down
13 changes: 13 additions & 0 deletions user_guide_src/source/incoming/routing/068.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Controllers;

class Catalog extends BaseController
{
public function productLookup(...$params)
{
echo $params[0] ?? null; // Will be 123 in all examples
echo $params[1] ?? null; // null in first, 456 in second and third example
echo $params[2] ?? null; // null in first and second, 789 in third
}
}

0 comments on commit ed3af11

Please sign in to comment.