Skip to content

Commit ec489c3

Browse files
committed
fix: reverse routing causes ErrorException
ErrorException : preg_match(): Compilation failed: missing closing parenthesis
1 parent ab63068 commit ec489c3

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

system/Router/RouteCollection.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,11 +1151,13 @@ public function environment(string $env, Closure $callback): RouteCollectionInte
11511151
public function reverseRoute(string $search, ...$params)
11521152
{
11531153
// Named routes get higher priority.
1154-
foreach ($this->routesNames as $collection) {
1154+
foreach ($this->routesNames as $verb => $collection) {
11551155
if (array_key_exists($search, $collection)) {
11561156
$routeKey = $collection[$search];
11571157

1158-
return $this->buildReverseRoute($routeKey, $params);
1158+
$from = $this->routes[$verb][$routeKey]['from'];
1159+
1160+
return $this->buildReverseRoute($from, $params);
11591161
}
11601162
}
11611163

@@ -1172,7 +1174,8 @@ public function reverseRoute(string $search, ...$params)
11721174
// all routes to find a match.
11731175
foreach ($this->routes as $collection) {
11741176
foreach ($collection as $routeKey => $route) {
1175-
$to = $route['handler'];
1177+
$to = $route['handler'];
1178+
$from = $route['from'];
11761179

11771180
// ignore closures
11781181
if (! is_string($to)) {
@@ -1196,7 +1199,7 @@ public function reverseRoute(string $search, ...$params)
11961199
continue;
11971200
}
11981201

1199-
return $this->buildReverseRoute($routeKey, $params);
1202+
return $this->buildReverseRoute($from, $params);
12001203
}
12011204
}
12021205

@@ -1311,21 +1314,21 @@ protected function fillRouteParams(string $from, ?array $params = null): string
13111314
* @param array $params One or more parameters to be passed to the route.
13121315
* The last parameter allows you to set the locale.
13131316
*/
1314-
protected function buildReverseRoute(string $routeKey, array $params): string
1317+
protected function buildReverseRoute(string $from, array $params): string
13151318
{
13161319
$locale = null;
13171320

13181321
// Find all of our back-references in the original route
1319-
preg_match_all('/\(([^)]+)\)/', $routeKey, $matches);
1322+
preg_match_all('/\(([^)]+)\)/', $from, $matches);
13201323

13211324
if (empty($matches[0])) {
1322-
if (strpos($routeKey, '{locale}') !== false) {
1325+
if (strpos($from, '{locale}') !== false) {
13231326
$locale = $params[0] ?? null;
13241327
}
13251328

1326-
$routeKey = $this->replaceLocale($routeKey, $locale);
1329+
$from = $this->replaceLocale($from, $locale);
13271330

1328-
return '/' . ltrim($routeKey, '/');
1331+
return '/' . ltrim($from, '/');
13291332
}
13301333

13311334
// Locale is passed?
@@ -1336,25 +1339,34 @@ protected function buildReverseRoute(string $routeKey, array $params): string
13361339

13371340
// Build our resulting string, inserting the $params in
13381341
// the appropriate places.
1339-
foreach ($matches[0] as $index => $pattern) {
1342+
foreach ($matches[0] as $index => $placeholder) {
13401343
if (! isset($params[$index])) {
13411344
throw new InvalidArgumentException(
1342-
'Missing argument for "' . $pattern . '" in route "' . $routeKey . '".'
1345+
'Missing argument for "' . $placeholder . '" in route "' . $from . '".'
13431346
);
13441347
}
1348+
1349+
$placeholderName = substr($placeholder, 2, -1);
1350+
if (isset($this->placeholders[$placeholderName])) {
1351+
$pattern = $this->placeholders[$placeholderName];
1352+
} else {
1353+
// The $placeholder is not a placeholder. It is a regex.
1354+
$pattern = $placeholder;
1355+
}
1356+
13451357
if (! preg_match('#^' . $pattern . '$#u', $params[$index])) {
13461358
throw RouterException::forInvalidParameterType();
13471359
}
13481360

13491361
// Ensure that the param we're inserting matches
13501362
// the expected param type.
1351-
$pos = strpos($routeKey, $pattern);
1352-
$routeKey = substr_replace($routeKey, $params[$index], $pos, strlen($pattern));
1363+
$pos = strpos($from, $placeholder);
1364+
$from = substr_replace($from, $params[$index], $pos, strlen($placeholder));
13531365
}
13541366

1355-
$routeKey = $this->replaceLocale($routeKey, $locale);
1367+
$from = $this->replaceLocale($from, $locale);
13561368

1357-
return '/' . ltrim($routeKey, '/');
1369+
return '/' . ltrim($from, '/');
13581370
}
13591371

13601372
/**

0 commit comments

Comments
 (0)