Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improve routing #8340

Merged
merged 4 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

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

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
2 changes: 1 addition & 1 deletion user_guide_src/source/incoming/routing/011.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Controllers;

class ProductController extends BaseController
class Catalog extends BaseController
{
public function productLookup($seg1 = false, $seg2 = false, $seg3 = false)
{
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/incoming/routing/045.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Routing extends BaseRouting
// ...
}

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

Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/incoming/routing/046.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

// Controller is \App\Users
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/incoming/routing/049.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class Routing extends BaseRouting
// ...
}

// This can be overridden in the Routes file
// This can be overridden in app/Config/Routes.php
$routes->setTranslateURIDashes(true);
2 changes: 1 addition & 1 deletion user_guide_src/source/incoming/routing/050.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class Routing extends BaseRouting
// ...
}

// This can be overridden in the Routes file
// This can be overridden in app/Config/Routes.php
$routes->setAutoRoute(false);
1 change: 1 addition & 0 deletions user_guide_src/source/incoming/routing/051.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Routing extends BaseRouting
// ...
}

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

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

// In app/Config/Routing.php
class Routing extends BaseRouting
{
// ...
public bool $prioritize = true;
// ...
}

// In app/Config/Routes.php
// to enable
$routes->setPrioritize();

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
}
}