Skip to content

Commit 27c6fcb

Browse files
committed
try fix alias
1 parent af22ce5 commit 27c6fcb

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

src/Http/Http.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,9 @@ public function execute(Route $route, Request $request): static
648648
{
649649
$arguments = [];
650650
$groups = $route->getGroups();
651-
$pathValues = $route->getPathValues($request);
651+
652+
$preparedPath = Router::preparePath($route->getMatchedPath());
653+
$pathValues = $route->getPathValues($request, $preparedPath[0]);
652654

653655
try {
654656
if ($route->getHook()) {

src/Http/Route.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Route extends Hook
3030
/**
3131
* Path params.
3232
*
33-
* @var array<string,string>
33+
* @var array<string,array<string, string>>
3434
*/
3535
protected array $pathParams = [];
3636

@@ -48,13 +48,25 @@ class Route extends Hook
4848
*/
4949
protected int $order;
5050

51+
protected string $matchedPath = '';
52+
5153
public function __construct(string $method, string $path)
5254
{
55+
parent::__construct();
5356
$this->path($path);
5457
$this->method = $method;
5558
$this->order = ++self::$counter;
56-
$this->action = function (): void {
57-
};
59+
}
60+
61+
public function setMatchedPath(string $path): self
62+
{
63+
$this->matchedPath = $path;
64+
return $this;
65+
}
66+
67+
public function getMatchedPath(): string
68+
{
69+
return $this->matchedPath;
5870
}
5971

6072
/**
@@ -143,23 +155,30 @@ public function getHook(): bool
143155
* @param int $index
144156
* @return void
145157
*/
146-
public function setPathParam(string $key, int $index): void
158+
public function setPathParam(string $key, int $index, string $path = ''): void
147159
{
148-
$this->pathParams[$key] = $index;
160+
$this->pathParams[$path][$key] = $index;
149161
}
150162

151163
/**
152164
* Get path params.
153165
*
154166
* @param \Utopia\Http\Request $request
167+
* @param string $path
155168
* @return array
156169
*/
157-
public function getPathValues(Request $request): array
170+
public function getPathValues(Request $request, string $path = ''): array
158171
{
159172
$pathValues = [];
160173
$parts = explode('/', ltrim($request->getURI(), '/'));
161174

162-
foreach ($this->pathParams as $key => $index) {
175+
if (empty($path)) {
176+
$pathParams = $this->pathParams[$path] ?? \array_values($this->pathParams)[0] ?? [];
177+
} else {
178+
$pathParams = $this->pathParams[$path] ?? [];
179+
}
180+
181+
foreach ($pathParams as $key => $index) {
163182
if (array_key_exists($index, $parts)) {
164183
$pathValues[$key] = $parts[$index];
165184
}

src/Http/Router.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static function addRoute(Route $route): void
8686
}
8787

8888
foreach ($params as $key => $index) {
89-
$route->setPathParam($key, $index);
89+
$route->setPathParam($key, $index, $path);
9090
}
9191

9292
self::$routes[$route->getMethod()][$path] = $route;
@@ -101,12 +101,16 @@ public static function addRoute(Route $route): void
101101
*/
102102
public static function addRouteAlias(string $path, Route $route): void
103103
{
104-
[$alias] = self::preparePath($path);
104+
[$alias, $params] = self::preparePath($path);
105105

106106
if (array_key_exists($alias, self::$routes[$route->getMethod()]) && !self::$allowOverride) {
107107
throw new Exception("Route for ({$route->getMethod()}:{$alias}) already registered.");
108108
}
109109

110+
foreach ($params as $key => $index) {
111+
$route->setPathParam($key, $index, $alias);
112+
}
113+
110114
self::$routes[$route->getMethod()][$alias] = $route;
111115
}
112116

@@ -123,7 +127,7 @@ public static function match(string $method, string $path): Route|null
123127
return null;
124128
}
125129

126-
$parts = array_values(array_filter(explode('/', $path)));
130+
$parts = array_values(array_filter(explode('/', $path), fn ($segment) => $segment !== ''));
127131
$length = count($parts) - 1;
128132
$filteredParams = array_filter(self::$params, fn ($i) => $i <= $length);
129133

@@ -138,7 +142,9 @@ public static function match(string $method, string $path): Route|null
138142
);
139143

140144
if (array_key_exists($match, self::$routes[$method])) {
141-
return self::$routes[$method][$match];
145+
$route = self::$routes[$method][$match];
146+
$route->setMatchedPath($match);
147+
return $route;
142148
}
143149
}
144150

@@ -147,7 +153,9 @@ public static function match(string $method, string $path): Route|null
147153
*/
148154
$match = self::WILDCARD_TOKEN;
149155
if (array_key_exists($match, self::$routes[$method])) {
150-
return self::$routes[$method][$match];
156+
$route = self::$routes[$method][$match];
157+
$route->setMatchedPath($match);
158+
return $route;
151159
}
152160

153161
/**
@@ -157,7 +165,9 @@ public static function match(string $method, string $path): Route|null
157165
$current = ($current ?? '') . "{$part}/";
158166
$match = $current . self::WILDCARD_TOKEN;
159167
if (array_key_exists($match, self::$routes[$method])) {
160-
return self::$routes[$method][$match];
168+
$route = self::$routes[$method][$match];
169+
$route->setMatchedPath($match);
170+
return $route;
161171
}
162172
}
163173

@@ -192,7 +202,7 @@ protected static function combinations(array $set): iterable
192202
* @param string $path
193203
* @return array
194204
*/
195-
protected static function preparePath(string $path): array
205+
public static function preparePath(string $path): array
196206
{
197207
$parts = array_values(array_filter(explode('/', $path)));
198208
$prepare = '';

0 commit comments

Comments
 (0)