From d0a026f5d912faacd17e8f6500dc717c0977e14b Mon Sep 17 00:00:00 2001 From: William Date: Wed, 26 Mar 2025 11:02:34 +0000 Subject: [PATCH] Update Route.php --- Route.php | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/Route.php b/Route.php index 3a9707d..6e38263 100644 --- a/Route.php +++ b/Route.php @@ -2,12 +2,31 @@ namespace WillySoft; -use Exception; +use \Exception; abstract class Route { - static $prefix = ''; - static $groups = [[]]; + static string $prefix = ''; + static array $groups = [ + [] + ]; + + private static function getRequestPath(): string + { + static $request_url_path; + + if (isset($request_url_path)) { + return $request_url_path; + } + + $request_url_path = substr_replace( + urldecode(strtok($_SERVER['REQUEST_URI'], '?')), '', 0, strlen($_SERVER['SCRIPT_NAME']) + ); + if ($request_url_path === '') + $request_url_path = '/'; + + return $request_url_path; + } static function match(string $methods, string $url_path, callable $callback) { @@ -16,9 +35,8 @@ abstract class Route array_map(strtoupper(...), explode('|', $methods)) )) return; - $request_url_path_parts = explode('/', urldecode( - strtok($_SERVER['REQUEST_URI'], '?') - )); + $request_url_path_parts = explode('/', self::getRequestPath()); + $url_path_parts = explode('/', self::$prefix . $url_path); $callback_args = []; for ($i = 1; $i < count($url_path_parts); $i++) { @@ -85,10 +103,10 @@ abstract class Route ); } - static function group(string $prefix, callable $callback) + static function group(string $prefix = '', callable $callback) { if (!str_starts_with( - $_SERVER['REQUEST_URI'], + self::getRequestPath(), self::$prefix . $prefix )) return; self::$prefix = self::$prefix . $prefix; @@ -97,4 +115,4 @@ abstract class Route array_pop(self::$groups); self::$prefix = rtrim(self::$prefix, $prefix); } -} +} \ No newline at end of file