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: [Commands] routes with {locale} in route #8152

Closed
Closed
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
18 changes: 17 additions & 1 deletion system/Commands/Utilities/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'];

Expand Down
28 changes: 28 additions & 0 deletions tests/system/Commands/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
6 changes: 3 additions & 3 deletions tests/system/Commands/Utilities/Routes/FilterFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down
11 changes: 11 additions & 0 deletions user_guide_src/source/incoming/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``<unknown>`` 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 <spark-filter-check>` command.

Auto Routing (Improved)
Expand Down
Loading