From fda45d6d3810e1c76541c7a1082471ae43034a3e Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 11 Feb 2024 06:20:35 +0900 Subject: [PATCH 1/3] feat: permit __invoke() method as Controller default method --- system/CodeIgniter.php | 5 ++++- tests/system/CodeIgniterTest.php | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 9fffa8bd408d..a8ccbcfc7062 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -879,7 +879,10 @@ protected function startController() } // Try to autoload the class - if (! class_exists($this->controller, true) || $this->method[0] === '_') { + if ( + ! class_exists($this->controller, true) + || ($this->method[0] === '_' && $this->method !== '__invoke') + ) { throw PageNotFoundException::forControllerNotFound($this->controller, $this->method); } } diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index 76007d28ba29..f54a84445e0d 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -13,7 +13,9 @@ namespace CodeIgniter; +use App\Controllers\Home; use CodeIgniter\Config\Services; +use CodeIgniter\Debug\Timer; use CodeIgniter\Exceptions\ConfigException; use CodeIgniter\Exceptions\PageNotFoundException; use CodeIgniter\HTTP\Method; @@ -960,4 +962,17 @@ public function testRunControllerNotFoundBeforeFilter(): void $this->codeigniter->run($routes); } + + public function testStartControllerPermitsInvoke(): void + { + $this->setPrivateProperty($this->codeigniter, 'benchmark', new Timer()); + $this->setPrivateProperty($this->codeigniter, 'controller', '\\' . Home::class); + $startController = $this->getPrivateMethodInvoker($this->codeigniter, 'startController'); + + $this->setPrivateProperty($this->codeigniter, 'method', '__invoke'); + $startController(); + + // No PageNotFoundException + $this->assertTrue(true); + } } From 70094cb069d85040b9355b88af9bddd282eea9c2 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 11 Feb 2024 09:49:52 +0900 Subject: [PATCH 2/3] docs: add user guide --- user_guide_src/source/changelogs/v4.5.0.rst | 2 ++ user_guide_src/source/incoming/routing.rst | 32 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index 591879b0ebfb..faae0dc76c9a 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -374,6 +374,8 @@ Others See :ref:`multiple-uri-segments-as-one-parameter` for details. - Now the 404 controller's method that you set in ``$override404`` also receive a ``PageNotFoundException`` message as the first parameter. + - Now you can use the ``__invoke()`` method as the default method. See + :ref:`routing-default-method`. - **Autoloader:** - Autoloading performance when using Composer has been improved. Adding the ``App`` namespace in the ``autoload.psr4`` setting in **composer.json** diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index 71ee8ad3e296..b2ab8e1e8686 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -50,7 +50,7 @@ Examples Here are a few basic routing examples. A URL containing the word **journals** in the first segment will be mapped to the ``\App\Controllers\Blogs`` class, -and the default method, which is usually ``index()``: +and the :ref:`default method `, which is usually ``index()``: .. literalinclude:: routing/006.php @@ -654,6 +654,32 @@ then you can change this value to save typing: .. literalinclude:: routing/046.php +.. _routing-default-method: + +Default Method +============== + +This setting is used when the route handler only has the controller name and no +method name listed. The default value is ``index``. +:: + + // In app/Config/Routing.php + public string $defaultMethod = 'index'; + +.. note:: The ``$defaultMethod`` is also common with Auto Routing. + See :ref:`Auto Routing (Improved) ` + or :ref:`Auto Routing (Legacy) `. + +If you define the following route:: + + $routes->get('/', 'Home'); + +the ``index()`` method of the ``App\Controllers\Home`` controller is executed +when the route matches. + +.. note:: Method names beginning with ``_`` cannot be used as the default method. + However, starting with v4.5.0, only ``__invoke`` can be used. + Translate URI Dashes ==================== @@ -828,6 +854,8 @@ in the controllers directory. For example, if the user visits **example.com/admi See :ref:`Auto Routing in Controllers ` for more info. +.. _routing-auto-routing-improved-default-method: + Default Method -------------- @@ -954,6 +982,8 @@ in the controllers directory. For example, if the user visits **example.com/admi See :ref:`Auto Routing (Legacy) in Controllers ` for more info. +.. _routing-auto-routing-legacy-default-method: + Default Method (Legacy) ----------------------- From e2ae3ef1260f77b6c3379e1ba7f9a9097bba08af Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 12 Feb 2024 08:40:59 +0900 Subject: [PATCH 3/3] docs: fix by proofreading Co-authored-by: Michal Sniatala --- 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 b2ab8e1e8686..8bd04e6982a5 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -678,7 +678,7 @@ the ``index()`` method of the ``App\Controllers\Home`` controller is executed when the route matches. .. note:: Method names beginning with ``_`` cannot be used as the default method. - However, starting with v4.5.0, only ``__invoke`` can be used. + However, starting with v4.5.0, ``__invoke`` method is allowed. Translate URI Dashes ====================