diff --git a/system/Router/DefinedRouteCollector.php b/system/Router/DefinedRouteCollector.php index 2b317d4cbc2a..f8e245d19723 100644 --- a/system/Router/DefinedRouteCollector.php +++ b/system/Router/DefinedRouteCollector.php @@ -57,7 +57,7 @@ public function collect(): Generator $handler = $view ? '(View) ' . $view : '(Closure)'; } - $routeName = $this->routeCollection->getRoutesOptions($route)['as'] ?? $route; + $routeName = $this->routeCollection->getRoutesOptions($route, $method)['as'] ?? $route; yield [ 'method' => $method, diff --git a/tests/system/Router/DefinedRouteCollectorTest.php b/tests/system/Router/DefinedRouteCollectorTest.php index ba74cec1a86a..976d74268a35 100644 --- a/tests/system/Router/DefinedRouteCollectorTest.php +++ b/tests/system/Router/DefinedRouteCollectorTest.php @@ -87,4 +87,45 @@ public function testCollect() ]; $this->assertSame($expected, $definedRoutes); } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/8039 + */ + public function testCollectSameFromWithDifferentVerb() + { + $routes = $this->createRouteCollection(); + $routes->get('login', 'AuthController::showLogin', ['as' => 'loginShow']); + $routes->post('login', 'AuthController::login', ['as' => 'login']); + $routes->get('logout', 'AuthController::logout', ['as' => 'logout']); + + $collector = new DefinedRouteCollector($routes); + + $definedRoutes = []; + + foreach ($collector->collect() as $route) { + $definedRoutes[] = $route; + } + + $expected = [ + [ + 'method' => 'get', + 'route' => 'login', + 'name' => 'loginShow', + 'handler' => '\\App\\Controllers\\AuthController::showLogin', + ], + [ + 'method' => 'get', + 'route' => 'logout', + 'name' => 'logout', + 'handler' => '\\App\\Controllers\\AuthController::logout', + ], + [ + 'method' => 'post', + 'route' => 'login', + 'name' => 'login', + 'handler' => '\\App\\Controllers\\AuthController::login', + ], + ]; + $this->assertSame($expected, $definedRoutes); + } } diff --git a/user_guide_src/source/changelogs/v4.4.2.rst b/user_guide_src/source/changelogs/v4.4.2.rst index beb5790fc8c1..bacfdf2a2e97 100644 --- a/user_guide_src/source/changelogs/v4.4.2.rst +++ b/user_guide_src/source/changelogs/v4.4.2.rst @@ -46,6 +46,7 @@ Bugs Fixed production mode or to display backtrace in json when an exception occurred. - **Forge:** Fixed a bug where adding a Primary Key to an existing table was ignored if there were no other Keys added too. +- **Routing:** Fixed a bug that ``spark routes`` may show incorrect route names. See the repo's `CHANGELOG.md `_