From 8896719eab904a2640e6dd3b4cdfd188cb7b8281 Mon Sep 17 00:00:00 2001 From: John Vincent Bonza Date: Thu, 29 Sep 2022 12:06:01 +0800 Subject: [PATCH] Fix routings and redirects --- src/Core/Response.php | 24 ++++++++++++++++++++++-- src/Core/Router.php | 40 ++++++++++++++++++++++++++++++++++++++++ src/Core/View.php | 4 ++++ 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/Core/Response.php b/src/Core/Response.php index d8409ec..d934bf6 100644 --- a/src/Core/Response.php +++ b/src/Core/Response.php @@ -21,15 +21,35 @@ public function Redirect($url = NULL) throw new NotFoundException(); } $url = ltrim($url, '/'); + $url = "?view=" . $url; if(empty($url)) { $url = './'; } - header("Location: " . $url); + $this->Header($url); + } + + public function RedirectExternalLink($url = NULL) { + if(is_null($url)) { + return FALSE; + } + $this->Header($url); } public function RedirectWithConfirmation($url = NULL) { if(is_null($url)) { - // Parameter error + return FALSE; } + $this->Header($url); + } + + public function DownloadFile($file = NULL) { + if(is_null($file)) { + return FALSE; + } + $this->Header($file); + } + + protected function Header($url) { + header('location: ' . $url); } } \ No newline at end of file diff --git a/src/Core/Router.php b/src/Core/Router.php index 302b4ef..a1857dd 100644 --- a/src/Core/Router.php +++ b/src/Core/Router.php @@ -124,4 +124,44 @@ public function RenderViewOnly($view, $params = []) { return Application::$app->view->RenderViewOnly($view, $params); } + + public function Route($url, $params = []) { + if(!array_key_exists($url, $this->GetRouteMap('get'))) { + return FALSE; + } + $url = explode('/', $url); + $url = array_filter($url); + $url = [...$url]; + $count = count($url); + if($count > 1) { + $temp = ""; + $isSet = TRUE; + $i = 0; + if(count($params) != count($url) - 1) { + return FALSE; + } + foreach($url as $u) { + if($isSet) { + $temp .= "?view=" . $u; + $isSet = FALSE; + } else { + $match = str_replace(['{', '}'], '', $u); + $temp .= "&" . $match . "=" . $params[$i]; + $i++; + } + } + $url = $temp; + } else { + $url = "?view=" . $url[0]; + } + return $url; + } + + public function PrintRoute($url, $params = []) { + $route = $this->Route($url, $params); + if(!$route) { + $route = '?view=error'; + } + echo $route; + } } \ No newline at end of file diff --git a/src/Core/View.php b/src/Core/View.php index 980655b..4d4fea6 100644 --- a/src/Core/View.php +++ b/src/Core/View.php @@ -41,4 +41,8 @@ public function RenderViewOnly($view, array $params) public function Render($view) { include_once Application::$ROOT_DIR . '/' . $this->rootDirectory . "$view.php"; } + + public static function Route($url, $params = []) { + return Application::$app->router->PrintRoute($url, $params); + } } \ No newline at end of file