Skip to content

Commit 2ee1ddc

Browse files
committed
fix(Router): handle {locale} placeholder in redirect routes
1 parent 59e5eea commit 2ee1ddc

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

‎system/Router/Router.php‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -457,20 +457,6 @@ protected function checkRoutes(string $uri): bool
457457

458458
// Does the RegEx match?
459459
if (preg_match('#^' . $routeKey . '$#u', $uri, $matches)) {
460-
// Is this route supposed to redirect to another?
461-
if ($this->collection->isRedirect($routeKey)) {
462-
// replacing matched route groups with references: post/([0-9]+) -> post/$1
463-
$redirectTo = preg_replace_callback('/(\([^\(]+\))/', static function (): string {
464-
static $i = 1;
465-
466-
return '$' . $i++;
467-
}, is_array($handler) ? key($handler) : $handler);
468-
469-
throw new RedirectException(
470-
preg_replace('#\A' . $routeKey . '\z#u', $redirectTo, $uri),
471-
$this->collection->getRedirectCode($routeKey),
472-
);
473-
}
474460
// Store our locale so CodeIgniter object can
475461
// assign it to the Request.
476462
if (str_contains($matchedKey, '{locale}')) {
@@ -491,6 +477,25 @@ protected function checkRoutes(string $uri): bool
491477
unset($matched);
492478
}
493479

480+
// Is this route supposed to redirect to another?
481+
if ($this->collection->isRedirect($matchedKey)) {
482+
// replacing matched route groups with references: post/([0-9]+) -> post/$1
483+
$redirectTo = preg_replace_callback('/(\([^\(]+\))/', static function (): string {
484+
static $i = 1;
485+
486+
return '$' . $i++;
487+
}, is_array($handler) ? key($handler) : $handler);
488+
489+
if (str_contains($matchedKey, '{locale}')) {
490+
$redirectTo = str_replace('{locale}', $this->detectedLocale, $redirectTo);
491+
}
492+
493+
throw new RedirectException(
494+
preg_replace('#\A' . $routeKey . '\z#u', $redirectTo, $uri),
495+
$this->collection->getRedirectCode($matchedKey),
496+
);
497+
}
498+
494499
// Are we using Closures? If so, then we need
495500
// to collect the params into an array
496501
// so it can be passed to the controller method later.

‎tests/system/Router/RouterTest.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,9 @@ public static function provideRedirectRoute(): iterable
976976
['post/(:num)', 'article/(:num)', 'alias', '/article/1', 'post/1', 'alias'],
977977
['post/(:num)/comment/(:any)', 'article/(:num)/(:any)', 'post/$1/comment/$2', '/article/1/2', 'post/1/comment/2', 'alias'],
978978
['post/(:segment)/comment/(:any)', 'article/(:num)/(:any)', 'alias', '/article/1/2', 'post/1/comment/2', 'alias'],
979+
['posts', '{locale}/articles', 'posts', '/en/articles', 'posts', 'alias'],
980+
['{locale}/posts', '{locale}/articles', '{locale}/posts', '/en/articles', 'en/posts', 'alias'],
981+
['{locale}/posts', '{locale}/articles', 'alias', '/en/articles', 'en/posts', 'alias'],
979982
];
980983
}
981984

0 commit comments

Comments
 (0)