Skip to content

Commit efcabb3

Browse files
authored
docs: Add info about Router::getMatchedRouteOptions() (#9441)
* docs: Add info about `Router::getMatchedRouteOptions()` * fix: Apply suggestions
1 parent 73d5d3f commit efcabb3

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

user_guide_src/source/incoming/routing.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,3 +1044,12 @@ This method returns a list of filters that are currently active for the route be
10441044
.. note:: The ``getFilters()`` method returns only the filters defined for the specific route.
10451045
It does not include global filters or those specified in the **app/Config/Filters.php** file.
10461046

1047+
Getting Matched Route Options for the Current Route
1048+
===================================================
1049+
1050+
When we're defining routes, they may have optional parameters: ``filter``, ``namespace``, ``hostname``, ``subdomain``, ``offset``, ``priority``, ``as``. All of them were described earlier above.
1051+
Additionally, if we use ``addRedirect()`` we can also expect the ``redirect`` key.
1052+
To access the values of these parameters, we can call ``Router::getMatchedRouteOptions()``. Here is an example of the returned array:
1053+
1054+
.. literalinclude:: routing/074.php
1055+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
// Get the router instance.
4+
/** @var \CodeIgniter\Router\Router $router */
5+
$router = service('router');
6+
$options = $router->getMatchedRouteOptions();
7+
8+
echo 'Route name: ' . $options['as'];
9+
10+
print_r($options);
11+
12+
// Route name: api:auth
13+
//
14+
// Array
15+
// (
16+
// [filter] => api-auth
17+
// [namespace] => App\API\v1
18+
// [hostname] => example.com
19+
// [subdomain] => api
20+
// [offset] => 1
21+
// [priority] => 1
22+
// [as] => api:auth
23+
// )

0 commit comments

Comments
 (0)