From db05b804370016390c9aff69b1e03f6c5a5a87cd Mon Sep 17 00:00:00 2001 From: Igor Ilic Date: Fri, 9 Jul 2021 21:25:36 +0200 Subject: [PATCH] Fix dynamic route detection (#8) Signed-off-by: Igor Ilic --- Routes.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Routes.php b/Routes.php index 18c3310..5b37f3e 100644 --- a/Routes.php +++ b/Routes.php @@ -198,9 +198,16 @@ public function handle() $arguments = []; if ( $route === false ) { $dynamic_routes = array_filter($this->routes[$method], fn($route) => !is_null($route['regex'] ?? NULL)); - foreach ( $dynamic_routes as $dynamic_route ) { + foreach ( $dynamic_routes as $routePath => $dynamic_route ) { + $countRouteSlashes = count(explode("/", $routePath)); + $countPathSlashes = count(explode('/', $path)); + + //TODO: Find a way to not check the number of / as it seems a bit hacky + if ( $countPathSlashes !== $countRouteSlashes ) continue; + if ( preg_match("/{$dynamic_route['regex']}/", $path) ) { $route = $dynamic_route; + preg_match_all("/{$dynamic_route['regex']}/", $path, $matches); if ( count($matches) > 1 ) array_shift($matches); $matches = array_map(fn($m) => $m[0], $matches);