Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down