From c45e7685a1ce6acb6a950c37e2c8257cfddeb3eb Mon Sep 17 00:00:00 2001 From: Igor Ilic Date: Wed, 31 Mar 2021 22:19:18 +0200 Subject: [PATCH] Fix route regex detection and passing of parameters Signed-off-by: Igor Ilic --- Routes.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Routes.php b/Routes.php index 6430075..5ca562b 100644 --- a/Routes.php +++ b/Routes.php @@ -50,6 +50,7 @@ public function add(string $url = "", callable|string|null $callback = NULL, arr if (str_contains($url, ":")) { $nUrl = preg_replace("/(:[\w\-_]+)/", "([\w\-\_\:]+)", $url); $nUrl = str_replace("/", "\/", $nUrl); + $nUrl .= "$"; } $this->routes[$url] = [ @@ -87,18 +88,18 @@ public function route(): bool { } foreach ($this->routes as $route) { - if (is_null($route["regex"]) === FALSE) { + if (!is_null($route["regex"])) { if (preg_match("/^{$route["regex"]}/", $url) === 1) { $urlIndex = $route["url"]; - preg_match_all("/^{$route["regex"]}-{$_SERVER['REQUEST_METHOD']}/", $url, $tmpParams); - preg_match_all("/^{$route["regex"]}-{$_SERVER['REQUEST_METHOD']}/", $route["url"], $paramNames); + preg_match_all("/^{$route["regex"]}/", $url, $tmpParams); + preg_match_all("/^{$route["regex"]}/", $route["url"], $paramNames); array_shift($tmpParams); array_shift($paramNames); $params = []; for ($x = 0; $x < count($paramNames); $x++) { - $params[str_replace(":", "", $paramNames[$x][0])] = $tmpParams[$x][0]; + $params[str_replace(":", "", $paramNames[$x][0] ?? "")] = $tmpParams[$x][0] ?? ""; } if (is_array($this->routes[$urlIndex]["params"])) {