Skip to content

Commit f18b8f2

Browse files
authored
Merge pull request #9064 from kenjis/fix-route-group-filters
fix: filters passed to the ``$routes->group()`` are not merged into the filters passed to the inner routes
2 parents 3e88f85 + e580856 commit f18b8f2

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

system/Router/RouteCollection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,12 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
14561456
$to = $this->processArrayCallableSyntax($from, $to);
14571457
}
14581458

1459+
// Merge group filters.
1460+
if (isset($options['filter'])) {
1461+
$currentFilter = (array) ($this->currentOptions['filter'] ?? []);
1462+
$options['filter'] = array_merge($currentFilter, (array) $options['filter']);
1463+
}
1464+
14591465
$options = array_merge($this->currentOptions ?? [], $options ?? []);
14601466

14611467
// Route priority detect

tests/system/Router/RouteCollectionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,22 @@ static function ($routes): void {
440440
$this->assertSame($expected, $routes->getRoutesOptions());
441441
}
442442

443+
public function testGroupFilterAndRouteFilter(): void
444+
{
445+
$routes = $this->getCollector();
446+
447+
$routes->group('admin', ['filter' => ['csrf']], static function ($routes): void {
448+
$routes->get('profile', 'Admin\Profile::index', ['filter' => ['honeypot']]);
449+
});
450+
451+
$expected = [
452+
'admin/profile' => [
453+
'filter' => ['csrf', 'honeypot'],
454+
],
455+
];
456+
$this->assertSame($expected, $routes->getRoutesOptions());
457+
}
458+
443459
public function testGroupingWorksWithEmptyStringPrefix(): void
444460
{
445461
$routes = $this->getCollector();

user_guide_src/source/changelogs/v4.5.4.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Deprecations
3030
Bugs Fixed
3131
**********
3232

33+
- **Routing:** Fixed a bug that filters passed to ``$routes->group()`` were not
34+
merged into filters passed to the inner routes.
3335
- **CURLRequest:** Fixed a bug preventing the use of strings for ``version`` in the config array
3436
when making requests.
3537

user_guide_src/source/incoming/routing.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,9 @@ run the filter before or after the controller. This is especially handy during a
564564

565565
The value for the filter must match one of the aliases defined within **app/Config/Filters.php**.
566566

567+
.. note:: Prior to v4.5.4, due to a bug, filters passed to the ``group()`` were
568+
not merged into the filters passed to the inner routes.
569+
567570
Setting Other Options
568571
=====================
569572

0 commit comments

Comments
 (0)