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}"
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), thespark routescommand does not output the required result.Behavior before (
<unknown>):Expected:
Anything else?
I think the solution should be here
CodeIgniter4/system/Router/Router.php
Lines 439 to 457 in 0cd3993
CodeIgniter4/system/Commands/Utilities/Routes.php
Lines 120 to 122 in 0cd3993
During the execution of the command, we get a route. Because {locale} will not be in the allowed