Skip to content

Commit a39495c

Browse files
Merge pull request #15 from infinitybrackets/fix/Routing
Fix routings and redirects
2 parents 2185b0d + 8896719 commit a39495c

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

src/Core/Response.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,35 @@ public function Redirect($url = NULL)
2121
throw new NotFoundException();
2222
}
2323
$url = ltrim($url, '/');
24+
$url = "?view=" . $url;
2425
if(empty($url)) {
2526
$url = './';
2627
}
27-
header("Location: " . $url);
28+
$this->Header($url);
29+
}
30+
31+
public function RedirectExternalLink($url = NULL) {
32+
if(is_null($url)) {
33+
return FALSE;
34+
}
35+
$this->Header($url);
2836
}
2937

3038
public function RedirectWithConfirmation($url = NULL) {
3139
if(is_null($url)) {
32-
// Parameter error
40+
return FALSE;
3341
}
42+
$this->Header($url);
43+
}
44+
45+
public function DownloadFile($file = NULL) {
46+
if(is_null($file)) {
47+
return FALSE;
48+
}
49+
$this->Header($file);
50+
}
51+
52+
protected function Header($url) {
53+
header('location: ' . $url);
3454
}
3555
}

src/Core/Router.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,44 @@ public function RenderViewOnly($view, $params = [])
124124
{
125125
return Application::$app->view->RenderViewOnly($view, $params);
126126
}
127+
128+
public function Route($url, $params = []) {
129+
if(!array_key_exists($url, $this->GetRouteMap('get'))) {
130+
return FALSE;
131+
}
132+
$url = explode('/', $url);
133+
$url = array_filter($url);
134+
$url = [...$url];
135+
$count = count($url);
136+
if($count > 1) {
137+
$temp = "";
138+
$isSet = TRUE;
139+
$i = 0;
140+
if(count($params) != count($url) - 1) {
141+
return FALSE;
142+
}
143+
foreach($url as $u) {
144+
if($isSet) {
145+
$temp .= "?view=" . $u;
146+
$isSet = FALSE;
147+
} else {
148+
$match = str_replace(['{', '}'], '', $u);
149+
$temp .= "&" . $match . "=" . $params[$i];
150+
$i++;
151+
}
152+
}
153+
$url = $temp;
154+
} else {
155+
$url = "?view=" . $url[0];
156+
}
157+
return $url;
158+
}
159+
160+
public function PrintRoute($url, $params = []) {
161+
$route = $this->Route($url, $params);
162+
if(!$route) {
163+
$route = '?view=error';
164+
}
165+
echo $route;
166+
}
127167
}

src/Core/View.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ public function RenderViewOnly($view, array $params)
4141
public function Render($view) {
4242
include_once Application::$ROOT_DIR . '/' . $this->rootDirectory . "$view.php";
4343
}
44+
45+
public static function Route($url, $params = []) {
46+
return Application::$app->router->PrintRoute($url, $params);
47+
}
4448
}

0 commit comments

Comments
 (0)