Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into 4.6
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 22, 2024
2 parents 7e58971 + 655bd1d commit 9b6636a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
33 changes: 33 additions & 0 deletions user_guide_src/source/incoming/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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.

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

// Get the router instance.
/** @var \CodeIgniter\Router\Router $router */
$router = service('router');

// Retrieve the fully qualified class name of the controller handling the current request.
$controller = $router->controllerName();

// Retrieve the method name being executed in the controller for the current request.
$method = $router->methodName();

echo 'Current Controller: ' . $controller . '<br>';
echo 'Current Method: ' . $method;
8 changes: 8 additions & 0 deletions user_guide_src/source/incoming/routing/072.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// Get the router instance.
/** @var \CodeIgniter\Router\Router $router */
$router = service('router');
$filters = $router->getFilters();

echo 'Active Filters for the Route: ' . implode(', ', $filters);
4 changes: 2 additions & 2 deletions user_guide_src/source/models/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9b6636a

Please sign in to comment.