From 80c129ab7e0dff5b36d43b08569c0ddfc77c439a Mon Sep 17 00:00:00 2001 From: Abdulbasit Rubeya Date: Fri, 21 Mar 2025 15:25:59 +0300 Subject: [PATCH 1/2] feat: param values to `request()` params --- src/Router.php | 63 ++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/src/Router.php b/src/Router.php index 213cd3c..3cf5a28 100644 --- a/src/Router.php +++ b/src/Router.php @@ -749,44 +749,37 @@ public static function findRoute( $uri = $uri ?? static::getCurrentUri(); $routes = $routes ?? static::$routes[\Leaf\Http\Request::getMethod()]; - foreach ($routes as $route) { - // Replace all curly braces matches {} into word patterns (like Laravel) - $route['pattern'] = preg_replace('/\/{(.*?)}/', '/(.*?)', $route['pattern']); - - // we have a match! - if (preg_match_all('#^' . $route['pattern'] . '$#', $uri, $matches, PREG_OFFSET_CAPTURE)) { - // Rework matches to only contain the matches, not the orig string - $matches = array_slice($matches, 1); - - // Extract the matched URL parameters (and only the parameters) - $params = array_map(function ($match, $index) use ($matches) { - // We have a following parameter: take the substring from the current param position until the next one's position (thank you PREG_OFFSET_CAPTURE) - if (isset($matches[$index + 1]) && isset($matches[$index + 1][0]) && $matches[$index + 1][0][1] != -1 && is_array($matches[$index + 1][0])) { - return trim(substr($match[0][0], 0, $matches[$index + 1][0][1] - $match[0][1]), '/'); - } + // Preserve existing $_GET parameters + $existingQueryParams = $_GET; - // Temporary fix for optional parameters - if (($match[0][1] ?? 1) === -1 && ($match[0][0] ?? null) === '') { - return; - } + // Extract query string and remove it from URI + $parsedUrl = parse_url($uri); + $uriPath = $parsedUrl['path'] ?? '/'; + parse_str($parsedUrl['query'] ?? '', $queryParams); - // We have no following parameters: return the whole lot - return isset($match[0][0]) ? trim($match[0][0], '/') : null; - }, $matches, array_keys($matches)); - - $paramsWithSlash = array_filter($params, function ($param) { - if (!$param) { - return false; - } - - return strpos($param, '/') !== false; - }); - - // if any of the params contain /, we should skip this route - if (!empty($paramsWithSlash)) { - continue; + foreach ($routes as $route) { + // Match named parameters in the pattern + preg_match_all('/{(\w+)}/', $route['pattern'], $paramNames); + $paramNames = $paramNames[1] ?? []; + + // Replace all curly braces {} with regex capture groups + $pattern = preg_replace('/\/{(.*?)}/', '/([^\/]+)', $route['pattern']); + + // Match current URI against the route pattern + if (preg_match('#^' . $pattern . '$#', $uriPath, $matches)) { + array_shift($matches); // Remove full match + + // Extract parameter values + $params = []; + foreach ($matches as $index => $value) { + $paramName = $paramNames[$index] ?? "var" . ($index + 1); + $params[$paramName] = trim($value, '/'); } + // Merge extracted route parameters with existing query parameters + $_GET = array_merge($existingQueryParams, $params); + + // Return matched route info $routeData = [ 'params' => $params, 'handler' => $route['handler'], @@ -796,7 +789,7 @@ public static function findRoute( $handledRoutes[] = $routeData; if ($returnFirst) { - break; + return [$routeData]; } } } From 95383b3584a43de4ab180ce9a69c6444c0ec9d00 Mon Sep 17 00:00:00 2001 From: Abdulbasit Rubeya Date: Fri, 21 Mar 2025 15:33:53 +0300 Subject: [PATCH 2/2] # feat: add param values to `request()` params --- src/Router.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Router.php b/src/Router.php index 3cf5a28..617282e 100644 --- a/src/Router.php +++ b/src/Router.php @@ -788,9 +788,7 @@ public static function findRoute( $handledRoutes[] = $routeData; - if ($returnFirst) { - return [$routeData]; - } + if ($returnFirst) break; } }