Skip to content

Commit

Permalink
Merge pull request #9064 from kenjis/fix-route-group-filters
Browse files Browse the repository at this point in the history
fix: filters passed to the ``$routes->group()`` are not merged into the filters passed to the inner routes
  • Loading branch information
kenjis authored Jul 26, 2024
2 parents 3e88f85 + e580856 commit f18b8f2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.5.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 3 additions & 0 deletions user_guide_src/source/incoming/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====================

Expand Down

0 comments on commit f18b8f2

Please sign in to comment.