Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: permit __invoke() method as Controller default method #8533

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
32 changes: 31 additions & 1 deletion user_guide_src/source/incoming/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <routing-default-method>`, which is usually ``index()``:

.. literalinclude:: routing/006.php

Expand Down Expand Up @@ -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) <routing-auto-routing-improved-default-method>`
or :ref:`Auto Routing (Legacy) <routing-auto-routing-legacy-default-method>`.

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, ``__invoke`` method is allowed.

Translate URI Dashes
====================

Expand Down Expand Up @@ -828,6 +854,8 @@ in the controllers directory. For example, if the user visits **example.com/admi

See :ref:`Auto Routing in Controllers <controller-auto-routing-improved>` for more info.

.. _routing-auto-routing-improved-default-method:

Default Method
--------------

Expand Down Expand Up @@ -954,6 +982,8 @@ in the controllers directory. For example, if the user visits **example.com/admi

See :ref:`Auto Routing (Legacy) in Controllers <controller-auto-routing-legacy>` for more info.

.. _routing-auto-routing-legacy-default-method:

Default Method (Legacy)
-----------------------

Expand Down
Loading