diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 322987dc791b..8fba39dc03be 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -1456,6 +1456,12 @@ protected function create(string $verb, string $from, $to, ?array $options = nul $to = $this->processArrayCallableSyntax($from, $to); } + // Merge group filters. + if (isset($options['filter'])) { + $currentFilter = (array) ($this->currentOptions['filter'] ?? []); + $options['filter'] = array_merge($currentFilter, (array) $options['filter']); + } + $options = array_merge($this->currentOptions ?? [], $options ?? []); // Route priority detect diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 89959cd5f33e..5c06154a5d31 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -440,6 +440,22 @@ static function ($routes): void { $this->assertSame($expected, $routes->getRoutesOptions()); } + public function testGroupFilterAndRouteFilter(): void + { + $routes = $this->getCollector(); + + $routes->group('admin', ['filter' => ['csrf']], static function ($routes): void { + $routes->get('profile', 'Admin\Profile::index', ['filter' => ['honeypot']]); + }); + + $expected = [ + 'admin/profile' => [ + 'filter' => ['csrf', 'honeypot'], + ], + ]; + $this->assertSame($expected, $routes->getRoutesOptions()); + } + public function testGroupingWorksWithEmptyStringPrefix(): void { $routes = $this->getCollector(); diff --git a/user_guide_src/source/changelogs/v4.5.4.rst b/user_guide_src/source/changelogs/v4.5.4.rst index 51a9fd74f407..d09413124ee0 100644 --- a/user_guide_src/source/changelogs/v4.5.4.rst +++ b/user_guide_src/source/changelogs/v4.5.4.rst @@ -30,6 +30,8 @@ Deprecations Bugs Fixed ********** +- **Routing:** Fixed a bug that filters passed to ``$routes->group()`` were not + merged into filters passed to the inner routes. - **CURLRequest:** Fixed a bug preventing the use of strings for ``version`` in the config array when making requests. diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index b822d4cce17e..90c99ad73b61 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -564,6 +564,9 @@ run the filter before or after the controller. This is especially handy during a The value for the filter must match one of the aliases defined within **app/Config/Filters.php**. +.. note:: Prior to v4.5.4, due to a bug, filters passed to the ``group()`` were + not merged into the filters passed to the inner routes. + Setting Other Options =====================