Skip to content

Bug: [Command] spark routes for routes containing {locale} return unknown filters #7997

Description

@neznaika0

PHP Version

8.2

CodeIgniter4 Version

4.4.1

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

Linux

Which server did you use?

cli-server (PHP built-in webserver)

What happened?

When using placeholder {locale} in Config/Routes.php and $routes->useSupportedLocalesOnly(true), the spark routes command does not output the required result.

// Home page with locale
$routes->get('{locale}/', 'Home::index', ['as' => 'homepage']);

// Home page without locale
$routes->get('/', 'Home::index', ['as' => 'default_homepage']);

Behavior before (<unknown>):

$ ./spark routes

CodeIgniter v4.4.1 Command Line Tool - Server Time: 2023-10-01 10:52:23 UTC+03:00

+--------+-----------------------------+--------------------+-------------------------------------+----------------+---------------+
| Method | Route                       | Name               | Handler                             | Before Filters | After Filters |
+--------+-----------------------------+--------------------+-------------------------------------+----------------+---------------+
| GET    | {locale}                    | homepage           | \App\Controllers\Home::index        | <unknown>      | <unknown>     |
| GET    | /                           | default_homepage   | \App\Controllers\Home::index        | csrf           |               |
+--------+-----------------------------+--------------------+-------------------------------------+----------------+---------------+

Expected:

$ ./spark routes

CodeIgniter v4.4.1 Command Line Tool - Server Time: 2023-10-01 10:52:23 UTC+03:00

+--------+-----------------------------+--------------------+-------------------------------------+----------------+---------------+
| Method | Route                       | Name               | Handler                             | Before Filters | After Filters |
+--------+-----------------------------+--------------------+-------------------------------------+----------------+---------------+
| GET    | {locale}                    | homepage           | \App\Controllers\Home::index        | csrf           |               |
| GET    | /                           | default_homepage   | \App\Controllers\Home::index        | csrf           |               |
+--------+-----------------------------+--------------------+-------------------------------------+----------------+---------------+

Anything else?

I think the solution should be here

// Store our locale so CodeIgniter object can
// assign it to the Request.
if (strpos($matchedKey, '{locale}') !== false) {
preg_match(
'#^' . str_replace('{locale}', '(?<locale>[^/]+)', $matchedKey) . '$#u',
$uri,
$matched
);
if ($this->collection->shouldUseSupportedLocalesOnly()
&& ! in_array($matched['locale'], config(App::class)->supportedLocales, true)) {
// Throw exception to prevent the autorouter, if enabled,
// from trying to find a route
throw PageNotFoundException::forLocaleNotSupported($matched['locale']);
}
$this->detectedLocale = $matched['locale'];
unset($matched);
}

foreach ($definedRouteCollector->collect() as $route) {
$sampleUri = $uriGenerator->get($route['route']);
$filters = $filterCollector->get($route['method'], $sampleUri);

During the execution of the command, we get a route. Because {locale} will not be in the allowed

// Route
array(4) {
  'method' =>
  string(3) "get"
  'route' =>
  string(13) "{locale}"
  'name' =>
  string(13) "homepage"
  'handler' =>
  string(28) "\App\Controllers\Home::index"
}

// sample URI
string(8) "{locale}"

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions