diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 90a9fdc619e0..da21ffffd4fe 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -680,7 +680,7 @@ public function displayPerformanceMetrics(string $output): string * @param RouteCollectionInterface $routes An collection interface to use in place * of the config file. * - * @return array + * @return string */ protected function tryToRouteIt(RouteCollectionInterface $routes = null) { diff --git a/system/Router/Router.php b/system/Router/Router.php index f2535d47b768..e10f3f27bad4 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -174,9 +174,9 @@ public function handle(string $uri = null) if ($this->checkRoutes($uri)) { - if ($this->collection->isFiltered($uri)) + if ($this->collection->isFiltered($this->matchedRoute[0])) { - $this->filterInfo = $this->collection->getFilterForRoute($uri); + $this->filterInfo = $this->collection->getFilterForRoute($this->matchedRoute[0]); } return $this->controller; diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 6a6bd201d497..61bf929b0cd3 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -285,6 +285,110 @@ public function testRouteWorksWithFilters() //-------------------------------------------------------------------- + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/1247 + */ + public function testGroupedResourceRoutesWithFilters() + { + $group = [ + 'api', + [ + 'namespace' => 'App\Controllers\Api', + 'filter' => 'api-auth', + ], + function (RouteCollection $routes) { + $routes->resource('posts', [ + 'controller' => 'PostController', + ]); + }, + ]; + + // GET + $this->collection->setHTTPVerb('get'); + + $this->collection->group(...$group); + + $router = new Router($this->collection); + + $router->handle('api/posts'); + + $this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName()); + $this->assertEquals('index', $router->methodName()); + $this->assertEquals('api-auth', $router->getFilter()); + + $router->handle('api/posts/new'); + + $this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName()); + $this->assertEquals('new', $router->methodName()); + $this->assertEquals('api-auth', $router->getFilter()); + + $router->handle('api/posts/50'); + + $this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName()); + $this->assertEquals('show', $router->methodName()); + $this->assertEquals('api-auth', $router->getFilter()); + + $router->handle('api/posts/50/edit'); + + $this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName()); + $this->assertEquals('edit', $router->methodName()); + $this->assertEquals('api-auth', $router->getFilter()); + + // POST + $this->collection->setHTTPVerb('post'); + + $this->collection->group(...$group); + + $router = new Router($this->collection); + + $router->handle('api/posts'); + + $this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName()); + $this->assertEquals('create', $router->methodName()); + $this->assertEquals('api-auth', $router->getFilter()); + + // PUT + $this->collection->setHTTPVerb('put'); + + $this->collection->group(...$group); + + $router = new Router($this->collection); + + $router->handle('api/posts/50'); + + $this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName()); + $this->assertEquals('update', $router->methodName()); + $this->assertEquals('api-auth', $router->getFilter()); + + // PATCH + $this->collection->setHTTPVerb('patch'); + + $this->collection->group(...$group); + + $router = new Router($this->collection); + + $router->handle('api/posts/50'); + + $this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName()); + $this->assertEquals('update', $router->methodName()); + $this->assertEquals('api-auth', $router->getFilter()); + + // DELETE + $this->collection->setHTTPVerb('delete'); + + $this->collection->group(...$group); + + $router = new Router($this->collection); + + $router->handle('api/posts/50'); + + $this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName()); + $this->assertEquals('delete', $router->methodName()); + $this->assertEquals('api-auth', $router->getFilter()); + } + + //-------------------------------------------------------------------- + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/1240 */