Update route dispatcher to use new collection find method

This commit is contained in:
Maarten 2024-11-26 13:48:38 +01:00
parent d0760ed95c
commit ab5fa6d69e

View file

@ -48,6 +48,13 @@ class RouteDispatcher
} }
} }
/**
* Dispatch the router dispatcher
*
* @param \Core\Http\Request $request
* @param array $routeCollection
* @return void
*/
public static function dispatch(Request $request, array $routeCollection): void public static function dispatch(Request $request, array $routeCollection): void
{ {
new self($request, $routeCollection); new self($request, $routeCollection);
@ -64,15 +71,9 @@ class RouteDispatcher
$url = $this->request->url(); $url = $this->request->url();
$method = $this->request->method(); $method = $this->request->method();
foreach ($this->routeCollection[$method] ?? [] as $routeRegex => $route) { $route = RouteCollection::find($method, $url);
if (preg_match($routeRegex, $url, $matches)) { if ($route) {
$params = array_filter( return $route;
$matches,
fn($key) => !is_numeric($key),
ARRAY_FILTER_USE_KEY
);
return array_merge($route, ['params' => $params]);
}
} }
throw new NotFoundHttpException(sprintf("No route found for: %s", $url)); throw new NotFoundHttpException(sprintf("No route found for: %s", $url));