Skip to content

Commit 9b6636a

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.6
2 parents 7e58971 + 655bd1d commit 9b6636a

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

user_guide_src/source/incoming/routing.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,3 +1002,36 @@ You can specify the host in the request URL with the ``--host`` option:
10021002
.. code-block:: console
10031003
10041004
php spark routes --host accounts.example.com
1005+
1006+
Getting Routing Information
1007+
***************************
1008+
1009+
In CodeIgniter 4, understanding and managing routing information is crucial for handling HTTP requests effectively.
1010+
This involves retrieving details about the active controller and method, as well as the filters applied to a specific route.
1011+
Below, we explore how to access this routing information to assist in tasks such as logging, debugging, or implementing conditional logic.
1012+
1013+
Retrieving the Current Controller/Method Names
1014+
==============================================
1015+
1016+
In some cases, you might need to determine which controller and method have been triggered by the current HTTP request.
1017+
This can be useful for logging, debugging, or conditional logic based on the active controller method.
1018+
1019+
CodeIgniter 4 provides a simple way to access the current route's controller and method names using the ``Router`` class. Here is an example:
1020+
1021+
.. literalinclude:: routing/071.php
1022+
1023+
This functionality is particularly useful when you need to dynamically interact with your controller or log which method is handling a particular request.
1024+
1025+
Getting Active Filters for the Current Route
1026+
============================================
1027+
1028+
:doc:`Filters <filters>` are a powerful feature that enables you to perform operations such as authentication, logging, and security checks before or after processing HTTP requests.
1029+
To access the active filters for a specific route, you can use the :php:meth:`CodeIgniter\\Router\\Router::getFilters()` method from the ``Router`` class.
1030+
1031+
This method returns a list of filters that are currently active for the route being processed:
1032+
1033+
.. literalinclude:: routing/072.php
1034+
1035+
.. note:: The ``getFilters()`` method returns only the filters defined for the specific route.
1036+
It does not include global filters or those specified in the **app/Config/Filters.php** file.
1037+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
// Get the router instance.
4+
/** @var \CodeIgniter\Router\Router $router */
5+
$router = service('router');
6+
7+
// Retrieve the fully qualified class name of the controller handling the current request.
8+
$controller = $router->controllerName();
9+
10+
// Retrieve the method name being executed in the controller for the current request.
11+
$method = $router->methodName();
12+
13+
echo 'Current Controller: ' . $controller . '<br>';
14+
echo 'Current Method: ' . $method;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Get the router instance.
4+
/** @var \CodeIgniter\Router\Router $router */
5+
$router = service('router');
6+
$filters = $router->getFilters();
7+
8+
echo 'Active Filters for the Route: ' . implode(', ', $filters);

user_guide_src/source/models/model.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,10 +1004,10 @@ beforeInsert **data** = the key/value pairs that are being inserted. If an
10041004
afterInsert **id** = the primary key of the new row, or 0 on failure.
10051005
**data** = the key/value pairs being inserted.
10061006
**result** = the results of the ``insert()`` method used through the Query Builder.
1007-
beforeUpdate **id** = the array of primary keys of the rows being updated.
1007+
beforeUpdate **id** = the array of primary keys of the rows being passed to the ``update()`` method.
10081008
**data** = the key/value pairs that are being updated. If an object or Entity class is passed to the
10091009
``update()`` method, it is first converted to an array.
1010-
afterUpdate **id** = the array of primary keys of the rows being updated.
1010+
afterUpdate **id** = the array of primary keys of the rows being passed to the ``update()`` method.
10111011
**data** = the key/value pairs being updated.
10121012
**result** = the results of the ``update()`` method used through the Query Builder.
10131013
beforeFind The name of the calling **method**, whether a **singleton** was requested, and these additional fields:

0 commit comments

Comments
 (0)