diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 181229cba6dd..ed517ca40671 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -1622,8 +1622,17 @@ public function getRegisteredControllers(?string $verb = '*'): array $controllers = []; if ($verb === '*') { - foreach ($this->defaultHTTPMethods as $tmpVerb) { - foreach ($this->routes[$tmpVerb] as $route) { + // Routes added via `add()` or `map()` are stored in the `*` bucket, + // so it must be iterated as well. + $verbs = [...$this->defaultHTTPMethods, '*']; + + foreach ($verbs as $tmpVerb) { + foreach ($this->routes[$tmpVerb] ?? [] as $routeKey => $route) { + // A redirect has no controller to register. + if (! isset($route['handler']) || $this->isRedirect($routeKey)) { + continue; + } + $controller = $this->getControllerName($route['handler']); if ($controller !== null) { $controllers[] = $controller; diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index d5a5dab8a9a3..0d902a11394d 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -2032,6 +2032,30 @@ public function testGetRegisteredControllersReturnsControllerByAddMethod(): void $this->assertSame($expects, $routes); } + public function testGetRegisteredControllersReturnsControllerByAddMethodWithWildcardVerb(): void + { + $collection = $this->getCollector(); + $collection->get('test', '\App\Controllers\Hello::get'); + $collection->add('hello', '\App\Controllers\Test::hello'); + $collection->add('feed', static function (): void { + }); + $collection->map([ + 'mapped' => '\App\Controllers\Mapped::index', + 'duplicate' => '\App\Controllers\Test::anotherMethod', + ]); + $collection->addRedirect('old-route', 'new-route'); + $collection->get('from', 'to', ['redirect' => 301]); + + $routes = $collection->getRegisteredControllers('*'); + + $expects = [ + '\App\Controllers\Hello', + '\App\Controllers\Test', + '\App\Controllers\Mapped', + ]; + $this->assertSame($expects, $routes); + } + public function testGetRegisteredControllersDoesNotReturnClosures(): void { $collection = $this->getCollector();