Skip to content

Commit 6b882e9

Browse files
authored
Merge pull request #5280 from kenjis/fix-module-route-filter
fix: module filters are not discovered when using route filters
2 parents 16c40a4 + a8890e1 commit 6b882e9

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

system/CodeIgniter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
358358
{
359359
$routeFilter = $this->tryToRouteIt($routes);
360360

361+
$uri = $this->determinePath();
362+
361363
// Start up the filters
362364
$filters = Services::filters();
363365

@@ -375,8 +377,6 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
375377
}
376378
}
377379

378-
$uri = $this->determinePath();
379-
380380
// Never run filters when running through Spark cli
381381
if (! defined('SPARKED')) {
382382
// Run "before" filters

system/Filters/Filters.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,16 @@ public function __construct($config, RequestInterface $request, ResponseInterfac
107107
$this->setResponse($response);
108108

109109
$this->modules = $modules ?? config('Modules');
110+
111+
if ($this->modules->shouldDiscover('filters')) {
112+
$this->discoverFilters();
113+
}
110114
}
111115

112116
/**
113117
* If discoverFilters is enabled in Config then system will try to
114118
* auto-discover custom filters files in Namespaces and allow access to
115-
* the config object via the variable $customfilters as with the routes file
119+
* the config object via the variable $filters as with the routes file
116120
*
117121
* Sample :
118122
* $filters->aliases['custom-auth'] = \Acme\Blob\Filters\BlobAuth::class;
@@ -211,12 +215,10 @@ public function run(string $uri, string $position = 'before')
211215
* The resulting $this->filters is an array of only filters
212216
* that should be applied to this request.
213217
*
214-
* We go ahead an process the entire tree because we'll need to
218+
* We go ahead and process the entire tree because we'll need to
215219
* run through both a before and after and don't want to double
216220
* process the rows.
217221
*
218-
* @param string $uri
219-
*
220222
* @return Filters
221223
*/
222224
public function initialize(?string $uri = null)
@@ -225,10 +227,6 @@ public function initialize(?string $uri = null)
225227
return $this;
226228
}
227229

228-
if ($this->modules->shouldDiscover('filters')) {
229-
$this->discoverFilters();
230-
}
231-
232230
$this->processGlobals($uri);
233231
$this->processMethods();
234232
$this->processFilters($uri);

tests/system/Filters/FiltersTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,24 @@ public function testCustomFiltersLoad()
458458
$this->assertSame('http://hellowworld.com', $request->url);
459459
}
460460

461+
/**
462+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4720
463+
*/
464+
public function testAllCustomFiltersAreDiscoveredInConstructor()
465+
{
466+
$_SERVER['REQUEST_METHOD'] = 'GET';
467+
468+
$config = [
469+
'aliases' => [],
470+
'globals' => [],
471+
];
472+
$filtersConfig = $this->createConfigFromArray(FiltersConfig::class, $config);
473+
$filters = $this->createFilters($filtersConfig);
474+
475+
$configFilters = $this->getPrivateProperty($filters, 'config');
476+
$this->assertContains('test-customfilter', array_keys($configFilters->aliases));
477+
}
478+
461479
public function testRunThrowsWithInvalidClassType()
462480
{
463481
$_SERVER['REQUEST_METHOD'] = 'GET';

0 commit comments

Comments
 (0)