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: Route placeholder (:any) with {locale} #6003

Merged
Merged
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
23 changes: 10 additions & 13 deletions system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,6 @@ protected function checkRoutes(string $uri): bool

// Loop through the route array looking for wildcards
foreach ($routes as $routeKey => $handler) {
// Reset localeSegment
$localeSegment = null;

$routeKey = $routeKey === '/'
? $routeKey
: ltrim($routeKey, '/ ');
Expand All @@ -414,12 +411,7 @@ protected function checkRoutes(string $uri): bool

// Are we dealing with a locale?
if (strpos($routeKey, '{locale}') !== false) {
$localeSegment = array_search('{locale}', preg_split('/[\/]*((^[a-zA-Z0-9])|\(([^()]*)\))*[\/]+/m', $routeKey), true);

// Replace it with a regex so it
// will actually match.
$routeKey = str_replace('/', '\/', $routeKey);
$routeKey = str_replace('{locale}', '[^\/]+', $routeKey);
$routeKey = str_replace('{locale}', '[^/]+', $routeKey);
}

// Does the RegEx match?
Expand All @@ -440,10 +432,15 @@ protected function checkRoutes(string $uri): bool
}
// Store our locale so CodeIgniter object can
// assign it to the Request.
if (isset($localeSegment)) {
// The following may be inefficient, but doesn't upset NetBeans :-/
$temp = (explode('/', $uri));
$this->detectedLocale = $temp[$localeSegment];
if (strpos($matchedKey, '{locale}') !== false) {
preg_match(
'#^' . str_replace('{locale}', '(?<locale>[^/]+)', $matchedKey) . '$#u',
$uri,
$matched
);

$this->detectedLocale = $matched['locale'];
unset($matched);
}

// Are we using Closures? If so, then we need
Expand Down
6 changes: 6 additions & 0 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected function setUp(): void
'books/(:num)/(:alpha)/(:num)' => 'Blog::show/$3/$1',
'closure/(:num)/(:alpha)' => static fn ($num, $str) => $num . '-' . $str,
'{locale}/pages' => 'App\Pages::list_all',
'test/(:any)/lang/{locale}' => 'App\Pages::list_all',
'admin/admins' => 'App\Admin\Admins::list_all',
'admin/admins/edit/(:any)' => 'App/Admin/Admins::edit_show/$1',
'/some/slash' => 'App\Slash::index',
Expand Down Expand Up @@ -402,6 +403,11 @@ public function testDetectsLocales()

$this->assertTrue($router->hasLocale());
$this->assertSame('fr', $router->getLocale());

$router->handle('test/123/lang/bg');

$this->assertTrue($router->hasLocale());
$this->assertSame('bg', $router->getLocale());
}

public function testRouteResource()
Expand Down