diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index 561cc737db97..340dde63e4c9 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -1002,3 +1002,36 @@ You can specify the host in the request URL with the ``--host`` option: .. code-block:: console php spark routes --host accounts.example.com + +Getting Routing Information +*************************** + +In CodeIgniter 4, understanding and managing routing information is crucial for handling HTTP requests effectively. +This involves retrieving details about the active controller and method, as well as the filters applied to a specific route. +Below, we explore how to access this routing information to assist in tasks such as logging, debugging, or implementing conditional logic. + +Retrieving the Current Controller/Method Names +============================================== + +In some cases, you might need to determine which controller and method have been triggered by the current HTTP request. +This can be useful for logging, debugging, or conditional logic based on the active controller method. + +CodeIgniter 4 provides a simple way to access the current route's controller and method names using the ``Router`` class. Here is an example: + +.. literalinclude:: routing/071.php + +This functionality is particularly useful when you need to dynamically interact with your controller or log which method is handling a particular request. + +Getting Active Filters for the Current Route +============================================ + +:doc:`Filters ` are a powerful feature that enables you to perform operations such as authentication, logging, and security checks before or after processing HTTP requests. +To access the active filters for a specific route, you can use the :php:meth:`CodeIgniter\\Router\\Router::getFilters()` method from the ``Router`` class. + +This method returns a list of filters that are currently active for the route being processed: + +.. literalinclude:: routing/072.php + +.. note:: The ``getFilters()`` method returns only the filters defined for the specific route. + It does not include global filters or those specified in the **app/Config/Filters.php** file. + diff --git a/user_guide_src/source/incoming/routing/071.php b/user_guide_src/source/incoming/routing/071.php new file mode 100644 index 000000000000..fe56c279871d --- /dev/null +++ b/user_guide_src/source/incoming/routing/071.php @@ -0,0 +1,14 @@ +controllerName(); + +// Retrieve the method name being executed in the controller for the current request. +$method = $router->methodName(); + +echo 'Current Controller: ' . $controller . '
'; +echo 'Current Method: ' . $method; diff --git a/user_guide_src/source/incoming/routing/072.php b/user_guide_src/source/incoming/routing/072.php new file mode 100644 index 000000000000..dbf695c57d86 --- /dev/null +++ b/user_guide_src/source/incoming/routing/072.php @@ -0,0 +1,8 @@ +getFilters(); + +echo 'Active Filters for the Route: ' . implode(', ', $filters); diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index 9057001ae785..1a08519ca55d 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -1004,10 +1004,10 @@ beforeInsert **data** = the key/value pairs that are being inserted. If an afterInsert **id** = the primary key of the new row, or 0 on failure. **data** = the key/value pairs being inserted. **result** = the results of the ``insert()`` method used through the Query Builder. -beforeUpdate **id** = the array of primary keys of the rows being updated. +beforeUpdate **id** = the array of primary keys of the rows being passed to the ``update()`` method. **data** = the key/value pairs that are being updated. If an object or Entity class is passed to the ``update()`` method, it is first converted to an array. -afterUpdate **id** = the array of primary keys of the rows being updated. +afterUpdate **id** = the array of primary keys of the rows being passed to the ``update()`` method. **data** = the key/value pairs being updated. **result** = the results of the ``update()`` method used through the Query Builder. beforeFind The name of the calling **method**, whether a **singleton** was requested, and these additional fields: