From a9698274cfc551ec4bdc7b04707b55d219ab4b6f Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sat, 17 Aug 2024 17:11:46 +0330 Subject: [PATCH 01/10] docs: add Retrieving the Controller and Method Names --- user_guide_src/source/incoming/routing.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index 90c99ad73b61..4e4cf08b30c1 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -151,6 +151,18 @@ a simple view: .. literalinclude:: routing/020.php +Retrieving the Controller and 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 ``Services::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. + Specifying Route Paths ====================== From d8953adb523567431bb18de1a23f1c87fe28db0f Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sat, 17 Aug 2024 17:12:37 +0330 Subject: [PATCH 02/10] docs: add e.g. for Retrieving the Controller and Method Names --- user_guide_src/source/incoming/routing/071.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 user_guide_src/source/incoming/routing/071.php 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..6afae85d1538 --- /dev/null +++ b/user_guide_src/source/incoming/routing/071.php @@ -0,0 +1,15 @@ +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; From 4259b47237affb24a9c17bb0da0214adee1ca0f3 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sat, 17 Aug 2024 17:20:10 +0330 Subject: [PATCH 03/10] style: run cs-fix for happy code style --- user_guide_src/source/incoming/routing/071.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/incoming/routing/071.php b/user_guide_src/source/incoming/routing/071.php index 6afae85d1538..2b78af318686 100644 --- a/user_guide_src/source/incoming/routing/071.php +++ b/user_guide_src/source/incoming/routing/071.php @@ -11,5 +11,5 @@ // Retrieve the method name being executed in the controller for the current request. $method = $router->methodName(); -echo "Current Controller: " . $controller . "
"; -echo "Current Method: " . $method; +echo 'Current Controller: ' . $controller . '
'; +echo 'Current Method: ' . $method; From dea8829720d7391242d43dabf86d146e91fbe20b Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sun, 18 Aug 2024 07:49:41 +0330 Subject: [PATCH 04/10] refactor: use recommended code for service --- user_guide_src/source/incoming/routing/071.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/user_guide_src/source/incoming/routing/071.php b/user_guide_src/source/incoming/routing/071.php index 2b78af318686..8f918796d177 100644 --- a/user_guide_src/source/incoming/routing/071.php +++ b/user_guide_src/source/incoming/routing/071.php @@ -1,9 +1,10 @@ controllerName(); From 2861340c17d5a622d5cbbdef1ce1eb9fb9acd869 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sun, 18 Aug 2024 07:49:07 +0330 Subject: [PATCH 05/10] fix: use Router in text Co-authored-by: kenjis --- user_guide_src/source/incoming/routing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index 4e4cf08b30c1..f221e306b8fc 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -157,7 +157,7 @@ Retrieving the Controller and 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 ``Services::router()`` class. Here is an example: +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 From b9a327db85c462a493d7e41e9082556d92edeb7a Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 19 Aug 2024 15:12:34 +0330 Subject: [PATCH 06/10] docs: update for easier to read Co-authored-by: kenjis --- user_guide_src/source/incoming/routing/071.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/user_guide_src/source/incoming/routing/071.php b/user_guide_src/source/incoming/routing/071.php index 8f918796d177..b6651f3eb82e 100644 --- a/user_guide_src/source/incoming/routing/071.php +++ b/user_guide_src/source/incoming/routing/071.php @@ -1,9 +1,6 @@ Date: Mon, 19 Aug 2024 18:02:31 +0330 Subject: [PATCH 07/10] docs: add new Getting Routing Information & Accessing Active Filters for a Route --- user_guide_src/source/incoming/routing/072.php | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 user_guide_src/source/incoming/routing/072.php 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..a1fc7b7058a3 --- /dev/null +++ b/user_guide_src/source/incoming/routing/072.php @@ -0,0 +1,8 @@ +getFilters(); + +echo 'Active Filters for the Route: ' . implode(', ', $filters); \ No newline at end of file From 962aecd5c518a1e7c224673b935df0a9cfb3c4ea Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 19 Aug 2024 18:05:36 +0330 Subject: [PATCH 08/10] style: run composer cs-fix --- user_guide_src/source/incoming/routing.rst | 45 ++++++++++++++----- .../source/incoming/routing/071.php | 1 + .../source/incoming/routing/072.php | 16 +++---- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index f221e306b8fc..2b62ff5f2785 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -151,18 +151,6 @@ a simple view: .. literalinclude:: routing/020.php -Retrieving the Controller and 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. - Specifying Route Paths ====================== @@ -1139,3 +1127,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 Controller and 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. + +Accessing Active Filters for a 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 configuration files. + diff --git a/user_guide_src/source/incoming/routing/071.php b/user_guide_src/source/incoming/routing/071.php index b6651f3eb82e..fe56c279871d 100644 --- a/user_guide_src/source/incoming/routing/071.php +++ b/user_guide_src/source/incoming/routing/071.php @@ -1,4 +1,5 @@ getFilters(); - -echo 'Active Filters for the Route: ' . implode(', ', $filters); \ No newline at end of file +getFilters(); + +echo 'Active Filters for the Route: ' . implode(', ', $filters); From febe5a48a27437aa3d9e57d918f91ef9600b99f9 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 20 Aug 2024 07:55:04 +0330 Subject: [PATCH 09/10] docs: apply suggestions from code review Co-authored-by: kenjis --- user_guide_src/source/incoming/routing.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index 2b62ff5f2785..6462eb429dce 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -1135,7 +1135,7 @@ In CodeIgniter 4, understanding and managing routing information is crucial for 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 Controller and Method Names +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. @@ -1147,7 +1147,7 @@ CodeIgniter 4 provides a simple way to access the current route's controller and This functionality is particularly useful when you need to dynamically interact with your controller or log which method is handling a particular request. -Accessing Active Filters for a Route +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. @@ -1158,5 +1158,5 @@ This method returns a list of filters that are currently active for the route be .. 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 configuration files. + It does not include global filters or those specified in the **app/Config/Filters.php** file. From 0093929f51a54a24289537dd650296663495f3ae Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 20 Aug 2024 07:56:45 +0330 Subject: [PATCH 10/10] docs: fix UG style --- user_guide_src/source/incoming/routing.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index 6462eb429dce..73d2e7642bcd 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -1136,7 +1136,7 @@ This involves retrieving details about the active controller and method, as well 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. @@ -1148,7 +1148,7 @@ CodeIgniter 4 provides a simple way to access the current route's controller and 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.