From 4a41dc6a37198b9c92058c5b16984cf8ec20f9d7 Mon Sep 17 00:00:00 2001 From: junilhwang Date: Tue, 24 Nov 2020 21:44:30 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20route=20=EC=9E=91=EC=97=85=20?= =?UTF-8?q?=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.php | 29 +++++++++++++++++++++++------ router.php | 8 ++++++++ src/core/Route.php | 24 ++++++++++++------------ 3 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 router.php diff --git a/index.php b/index.php index 54a1298..93f66b2 100644 --- a/index.php +++ b/index.php @@ -10,12 +10,29 @@ use src\core\Route; -Route::add("GET", "/webskills/", "MainController::goToMain"); -Route::add("GET", "/webskills/main", "MainController::main"); -Route::add("GET", "/webskills/src/user/account/login", "UserController::getLogin"); -Route::add("GET", "/webskills/src/user/account/register", "UserController::getRegister"); +function test () { + echo "test"; +}; -Route::add("POST", "/webskills/src/user/account/login", "UserController::postLogin"); -Route::add("POST", "/webskills/src/user/account/register", "UserController::postRegister"); +function test2 () { + echo "test2"; +}; + + +function test3 () { + echo "test3"; +}; + +Route::add("GET", "/", "test"); +Route::add("GET", "/test", "test"); +Route::add("GET", "/test2", "test"); +Route::add("GET", "/test3/([0-9]+)", "test3"); +//Route::add("GET", "/webskills/", "MainController::goToMain"); +//Route::add("GET", "/webskills/main", "MainController::main"); +//Route::add("GET", "/webskills/src/user/account/login", "UserController::getLogin"); +//Route::add("GET", "/webskills/src/user/account/register", "UserController::getRegister"); +// +//Route::add("POST", "/webskills/src/user/account/login", "UserController::postLogin"); +//Route::add("POST", "/webskills/src/user/account/register", "UserController::postRegister"); Route::run(); \ No newline at end of file diff --git a/router.php b/router.php new file mode 100644 index 0000000..19fb3e1 --- /dev/null +++ b/router.php @@ -0,0 +1,8 @@ + self::match($route))); + preg_match_all(); + return call_user_func($route["callback"], ...$params); } - private static function match($method, $path) { - return $method === $_SERVER["REQUEST_METHOD"] && $path === $_SERVER["REQUEST_URI"] ?? "/"; + private static function match($route) { + $uri = str_replace("/", "\/", $route['uri'] ?? '/'); + return $route['method'] === $_SERVER["REQUEST_METHOD"] && + preg_match("/^{$uri}$/", $_SERVER["REQUEST_URI"]); } -}; -?> \ No newline at end of file +} \ No newline at end of file From ac4efb55d3e8a60af36083be9e83e14174489f25 Mon Sep 17 00:00:00 2001 From: junilhwang Date: Tue, 24 Nov 2020 21:54:52 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20route=20=EC=9E=91=EC=97=85=20?= =?UTF-8?q?=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.php | 7 +++++-- src/core/Route.php | 13 +++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/index.php b/index.php index 93f66b2..a9dc3b5 100644 --- a/index.php +++ b/index.php @@ -19,8 +19,11 @@ function test2 () { }; -function test3 () { - echo "test3"; +function test3 ($request) { + echo "
";
+    print_r($request);
+    echo "
"; + echo $request['params'][1]; }; Route::add("GET", "/", "test"); diff --git a/src/core/Route.php b/src/core/Route.php index d4d1ac0..c0e919f 100644 --- a/src/core/Route.php +++ b/src/core/Route.php @@ -6,20 +6,21 @@ class Route{ private static array $routes = []; public static function add($method, $uri, $callback) { - $uriReg = str_replace("/", "\/", $route['uri'] ?? '/'); + $uriReg = str_replace("/", "\/", $uri ?? '/'); + $uriReg = "/^{$uriReg}$/"; self::$routes[] = compact("method", "uriReg", "callback"); } public static function run() { $route = current(array_filter(self::$routes, fn($route) => self::match($route))); - preg_match_all(); - return call_user_func($route["callback"], ...$params); + preg_match_all($route['uriReg'], $_SERVER['REQUEST_URI'], $params, 0); + $request = ['params' => $params]; + return call_user_func($route["callback"], $request); } private static function match($route) { - $uri = str_replace("/", "\/", $route['uri'] ?? '/'); - return $route['method'] === $_SERVER["REQUEST_METHOD"] && - preg_match("/^{$uri}$/", $_SERVER["REQUEST_URI"]); + return $route['method'] === $_SERVER["REQUEST_METHOD"] && + preg_match($route['uriReg'], $_SERVER["REQUEST_URI"]); } } \ No newline at end of file From 1bab2953efe2726be7e0efc03d704d6269ff97af Mon Sep 17 00:00:00 2001 From: junilhwang Date: Tue, 24 Nov 2020 22:02:05 +0900 Subject: [PATCH 3/6] =?UTF-8?q?fix:=20EOF=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/Route.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/Route.php b/src/core/Route.php index c0e919f..e1cd2d6 100644 --- a/src/core/Route.php +++ b/src/core/Route.php @@ -23,4 +23,4 @@ private static function match($route) { preg_match($route['uriReg'], $_SERVER["REQUEST_URI"]); } -} \ No newline at end of file +} From ca7af1e7e468f8d5ca2a228fc9ee99a0c940efec Mon Sep 17 00:00:00 2001 From: junilhwang Date: Tue, 24 Nov 2020 22:02:31 +0900 Subject: [PATCH 4/6] =?UTF-8?q?fix:=20EOF=20=EC=A0=81=EC=9A=A9=20(2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.php | 2 +- router.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index a9dc3b5..c1d3f47 100644 --- a/index.php +++ b/index.php @@ -38,4 +38,4 @@ function test3 ($request) { //Route::add("POST", "/webskills/src/user/account/login", "UserController::postLogin"); //Route::add("POST", "/webskills/src/user/account/register", "UserController::postRegister"); -Route::run(); \ No newline at end of file +Route::run(); diff --git a/router.php b/router.php index 19fb3e1..ba64aed 100644 --- a/router.php +++ b/router.php @@ -5,4 +5,4 @@ } $_GET['url'] = $_SERVER['REQUEST_URI']; - include_once("./index.php"); \ No newline at end of file + include_once("./index.php"); From 9af22eae29f0571ab7672da166754d87bd2a47c6 Mon Sep 17 00:00:00 2001 From: junilhwang Date: Tue, 24 Nov 2020 22:04:18 +0900 Subject: [PATCH 5/6] =?UTF-8?q?format:=20indent=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/Route.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/core/Route.php b/src/core/Route.php index e1cd2d6..654f27e 100644 --- a/src/core/Route.php +++ b/src/core/Route.php @@ -5,22 +5,22 @@ class Route{ private static array $routes = []; - public static function add($method, $uri, $callback) { + public static function add($method, $uri, $callback) { $uriReg = str_replace("/", "\/", $uri ?? '/'); $uriReg = "/^{$uriReg}$/"; - self::$routes[] = compact("method", "uriReg", "callback"); - } + self::$routes[] = compact("method", "uriReg", "callback"); + } - public static function run() { - $route = current(array_filter(self::$routes, fn($route) => self::match($route))); + public static function run() { + $route = current(array_filter(self::$routes, fn($route) => self::match($route))); preg_match_all($route['uriReg'], $_SERVER['REQUEST_URI'], $params, 0); $request = ['params' => $params]; - return call_user_func($route["callback"], $request); - } + return call_user_func($route["callback"], $request); + } - private static function match($route) { + private static function match($route) { return $route['method'] === $_SERVER["REQUEST_METHOD"] && preg_match($route['uriReg'], $_SERVER["REQUEST_URI"]); - } + } } From c594a4ffd7c3d81c3e60836e305360cd0405404f Mon Sep 17 00:00:00 2001 From: junilhwang Date: Tue, 24 Nov 2020 22:06:00 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20index.php=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.php b/index.php index c1d3f47..485ade8 100644 --- a/index.php +++ b/index.php @@ -23,12 +23,11 @@ function test3 ($request) { echo "
";
     print_r($request);
     echo "
"; - echo $request['params'][1]; }; Route::add("GET", "/", "test"); Route::add("GET", "/test", "test"); -Route::add("GET", "/test2", "test"); +Route::add("GET", "/test2", "test2"); Route::add("GET", "/test3/([0-9]+)", "test3"); //Route::add("GET", "/webskills/", "MainController::goToMain"); //Route::add("GET", "/webskills/main", "MainController::main");