diff --git a/system/Commands/Utilities/Routes.php b/system/Commands/Utilities/Routes.php index 5d20da07d514..a78b6adaf425 100644 --- a/system/Commands/Utilities/Routes.php +++ b/system/Commands/Utilities/Routes.php @@ -18,6 +18,7 @@ use CodeIgniter\Commands\Utilities\Routes\FilterCollector; use CodeIgniter\Commands\Utilities\Routes\SampleURIGenerator; use CodeIgniter\Router\DefinedRouteCollector; +use Config\App; use Config\Feature; use Config\Routing; use Config\Services; @@ -119,7 +120,22 @@ public function run(array $params) foreach ($definedRouteCollector->collect() as $route) { $sampleUri = $uriGenerator->get($route['route']); - $filters = $filterCollector->get($route['method'], $sampleUri); + + // Fix for the search filters command + $isSupportedLocaleOnly = false; + + if (strpos($sampleUri, '{locale}') !== false && Services::routes()->shouldUseSupportedLocalesOnly()) { + $isSupportedLocaleOnly = true; + + $sampleUri = str_replace('{locale}', config(App::class)->defaultLocale, $sampleUri); + } + + $filters = $filterCollector->get($route['method'], $sampleUri); + + if ($isSupportedLocaleOnly) { + $filters['before'] = array_map(static fn ($filter) => '!' . $filter, $filters['before']); + $filters['after'] = array_map(static fn ($filter) => '!' . $filter, $filters['after']); + } $routeName = ($route['route'] === $route['name']) ? '»' : $route['name']; diff --git a/tests/system/Commands/RoutesTest.php b/tests/system/Commands/RoutesTest.php index 9cd24cbe0f6b..fcb3906bcd38 100644 --- a/tests/system/Commands/RoutesTest.php +++ b/tests/system/Commands/RoutesTest.php @@ -223,4 +223,32 @@ public function testRoutesCommandRouteLegacy(): void EOL; $this->assertStringContainsString($expected, $this->getBuffer()); } + + public function testRoutesCommandWithAnyLocales(): void + { + $routes = $this->getCleanRoutes(); + $routes->useSupportedLocalesOnly(false); + $routes->get('{locale}/admin/(:segment)', 'AdminController::index/$1', ['as' => 'admin']); + + command('routes'); + + $expected = <<<'EOL' + | GET | {locale}/admin/([^/]+) | admin | \App\Controllers\AdminController::index/$1 | | toolbar | + EOL; + $this->assertStringContainsString($expected, $this->getBuffer()); + } + + public function testRoutesCommandWithSupportedLocalesOnly(): void + { + $routes = $this->getCleanRoutes(); + $routes->useSupportedLocalesOnly(true); + $routes->get('{locale}/admin/(:segment)', 'AdminController::index/$1', ['as' => 'admin']); + + command('routes'); + + $expected = <<<'EOL' + | GET | {locale}/admin/([^/]+) | admin | \App\Controllers\AdminController::index/$1 | | !toolbar | + EOL; + $this->assertStringContainsString($expected, $this->getBuffer()); + } } diff --git a/tests/system/Commands/Utilities/Routes/FilterFinderTest.php b/tests/system/Commands/Utilities/Routes/FilterFinderTest.php index c59c9b9aee8e..d607e46a38b9 100644 --- a/tests/system/Commands/Utilities/Routes/FilterFinderTest.php +++ b/tests/system/Commands/Utilities/Routes/FilterFinderTest.php @@ -137,7 +137,7 @@ public function testFindGlobalsFiltersWithRedirectRoute(): void public function testFindGlobalsAndRouteFilters(): void { $collection = $this->createRouteCollection(); - $collection->get('admin', ' AdminController::index', ['filter' => 'honeypot']); + $collection->get('admin', 'AdminController::index', ['filter' => 'honeypot']); $router = $this->createRouter($collection); $filters = $this->createFilters(); @@ -155,7 +155,7 @@ public function testFindGlobalsAndRouteFilters(): void public function testFindGlobalsAndRouteClassnameFilters(): void { $collection = $this->createRouteCollection(); - $collection->get('admin', ' AdminController::index', ['filter' => InvalidChars::class]); + $collection->get('admin', 'AdminController::index', ['filter' => InvalidChars::class]); $router = $this->createRouter($collection); $filters = $this->createFilters(); @@ -173,7 +173,7 @@ public function testFindGlobalsAndRouteClassnameFilters(): void public function testFindGlobalsAndRouteMultipleFilters(): void { $collection = $this->createRouteCollection(); - $collection->get('admin', ' AdminController::index', ['filter' => ['honeypot', InvalidChars::class]]); + $collection->get('admin', 'AdminController::index', ['filter' => ['honeypot', InvalidChars::class]]); $router = $this->createRouter($collection); $filters = $this->createFilters(); diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index 39b5d3db8cdd..5dd50d9f7d94 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -923,6 +923,17 @@ The *Route* column shows the route path to match. The route of a defined route i Since v4.3.0, the *Name* column shows the route name. ``»`` indicates the name is the same as the route path. +Prior to v4.5.0, routes with ``{locale}`` and the enabled parameter ``$routes->shouldUseSupportedLocalesOnly(true)`` in **app/Config/Routes.php** were shown ```` in the *Filters* column. This was due to the fact that it was impossible to resolve filters for all languages. +Now routes having ``{locale}`` are output as ``!filtername``. This means that filters are set for a group of languages, but there is no guarantee of execution for each language. + +.. code-block:: none + + +--------+---------------+------+-----------+----------------+---------------+ + | Method | Route | Name | Handler | Before Filters | After Filters | + +--------+---------------+------+-----------+----------------+---------------+ + | GET | {locale}/feed | » | (Closure) | | !toolbar | + +--------+---------------+------+-----------+----------------+---------------+ + .. important:: The system is not perfect. If you use Custom Placeholders, *Filters* might not be correct. If you want to check filters for a route, you can use :ref:`spark filter:check ` command. Auto Routing (Improved)