diff --git a/src/Route.php b/src/Route.php index aa06a29..714b0a7 100755 --- a/src/Route.php +++ b/src/Route.php @@ -154,7 +154,7 @@ public function getHook(): bool * @param int $index * @return void */ - public function setPathParam(string $path, string $key, int $index): void + public function setPathParam(string $key, int $index, string $path = ''): void { $this->pathParams[$path][$key] = $index; } @@ -165,12 +165,18 @@ public function setPathParam(string $path, string $key, int $index): void * @param \Utopia\Request $request * @return array */ - public function getPathValues(Request $request, string $path): array + public function getPathValues(Request $request, string $path = ''): array { $pathValues = []; $parts = explode('/', ltrim($request->getURI(), '/')); - foreach (($this->pathParams[$path] ?? []) as $key => $index) { + if (empty($path)) { + $pathParams = $this->pathParams[$path] ?? \array_values($this->pathParams)[0] ?? []; + } else { + $pathParams = $this->pathParams[$path] ?? []; + } + + foreach ($pathParams as $key => $index) { if (array_key_exists($index, $parts)) { $pathValues[$key] = $parts[$index]; } diff --git a/src/Router.php b/src/Router.php index bc6e63c..eb2cea8 100644 --- a/src/Router.php +++ b/src/Router.php @@ -86,7 +86,7 @@ public static function addRoute(Route $route): void } foreach ($params as $key => $index) { - $route->setPathParam($path, $key, $index); + $route->setPathParam($key, $index, $path); } self::$routes[$route->getMethod()][$path] = $route; @@ -108,7 +108,7 @@ public static function addRouteAlias(string $path, Route $route): void } foreach ($params as $key => $index) { - $route->setPathParam($alias, $key, $index); + $route->setPathParam($key, $index, $alias); } self::$routes[$route->getMethod()][$alias] = $route;