fix(router): trim URI slash to match same routes for URIs with and without trailing slash
This commit is contained in:
parent
ab330e773e
commit
9e9375f9a2
|
@ -38,40 +38,40 @@ class Router extends CodeIgniterRouter
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$uri = $uri === '/' ? $uri : ltrim($uri, '/ ');
|
$uri = $uri === '/' ? $uri : trim($uri, '/ ');
|
||||||
|
|
||||||
// Loop through the route array looking for wildcards
|
// Loop through the route array looking for wildcards
|
||||||
foreach ($routes as $key => $val) {
|
foreach ($routes as $routeKey => $val) {
|
||||||
// Reset localeSegment
|
// Reset localeSegment
|
||||||
$localeSegment = null;
|
$localeSegment = null;
|
||||||
|
|
||||||
$key = $key === '/' ? $key : ltrim($key, '/ ');
|
$routeKey = $routeKey === '/' ? $routeKey : ltrim($routeKey, '/ ');
|
||||||
|
|
||||||
$matchedKey = $key;
|
$matchedKey = $routeKey;
|
||||||
|
|
||||||
// Are we dealing with a locale?
|
// Are we dealing with a locale?
|
||||||
if (str_contains($key, '{locale}')) {
|
if (str_contains($routeKey, '{locale}')) {
|
||||||
$localeSegment = array_search(
|
$localeSegment = array_search(
|
||||||
'{locale}',
|
'{locale}',
|
||||||
preg_split('~[\/]*((^[a-zA-Z0-9])|\(([^()]*)\))*[\/]+~m', $key),
|
preg_split('~[\/]*((^[a-zA-Z0-9])|\(([^()]*)\))*[\/]+~m', $routeKey),
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Replace it with a regex so it
|
// Replace it with a regex so it
|
||||||
// will actually match.
|
// will actually match.
|
||||||
$key = str_replace('/', '\/', $key);
|
$routeKey = str_replace('/', '\/', $routeKey);
|
||||||
$key = str_replace('{locale}', '[^\/]+', $key);
|
$routeKey = str_replace('{locale}', '[^\/]+', $routeKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Does the RegEx match?
|
// Does the RegEx match?
|
||||||
if (preg_match('#^' . $key . '$#u', $uri, $matches)) {
|
if (preg_match('#^' . $routeKey . '$#u', $uri, $matches)) {
|
||||||
$this->matchedRouteOptions = $this->collection->getRoutesOptions($matchedKey);
|
$this->matchedRouteOptions = $this->collection->getRoutesOptions($matchedKey);
|
||||||
|
|
||||||
// Is this route supposed to redirect to another?
|
// Is this route supposed to redirect to another?
|
||||||
if ($this->collection->isRedirect($key)) {
|
if ($this->collection->isRedirect($routeKey)) {
|
||||||
throw new RedirectException(
|
throw new RedirectException(
|
||||||
is_array($val) ? key($val) : $val,
|
is_array($val) ? key($val) : $val,
|
||||||
$this->collection->getRedirectCode($key),
|
$this->collection->getRedirectCode($routeKey),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,14 +160,14 @@ class Router extends CodeIgniterRouter
|
||||||
// ex: $routes->resource('Admin/Admins');
|
// ex: $routes->resource('Admin/Admins');
|
||||||
if (
|
if (
|
||||||
str_contains($val, '$') &&
|
str_contains($val, '$') &&
|
||||||
str_contains($key, '(') &&
|
str_contains($routeKey, '(') &&
|
||||||
str_contains($key, '/')
|
str_contains($routeKey, '/')
|
||||||
) {
|
) {
|
||||||
$replacekey = str_replace('/(.*)', '', $key);
|
$replacekey = str_replace('/(.*)', '', $routeKey);
|
||||||
$val = preg_replace('#^' . $key . '$#u', $val, $uri);
|
$val = preg_replace('#^' . $routeKey . '$#u', $val, $uri);
|
||||||
$val = str_replace($replacekey, str_replace('/', '\\', $replacekey), $val);
|
$val = str_replace($replacekey, str_replace('/', '\\', $replacekey), $val);
|
||||||
} elseif (str_contains($val, '$') && str_contains($key, '(')) {
|
} elseif (str_contains($val, '$') && str_contains($routeKey, '(')) {
|
||||||
$val = preg_replace('#^' . $key . '$#u', $val, $uri);
|
$val = preg_replace('#^' . $routeKey . '$#u', $val, $uri);
|
||||||
} elseif (str_contains($val, '/')) {
|
} elseif (str_contains($val, '/')) {
|
||||||
[$controller, $method] = explode('::', $val);
|
[$controller, $method] = explode('::', $val);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue