Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filters passed to the $routes->group() are not merged into the filters passed to the inner routes #9064

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading