-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter.php
executable file
·123 lines (96 loc) · 3.03 KB
/
Router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/** Class ROUTER */
namespace app;
use app\Database;
class Router
{
public array $getRoutes = [];
public array $postRoutes = [];
public Database $db;
public function __construct()
{
$this->db = new Database();
}
public function get($url, $fn)
{
//var_dump('getfn', $url);
$this->getRoutes[$url] = $fn;
}
public function post($url, $fn)
{
$this->postRoutes[$url] = $fn;
}
public function resolve()
{
$currentUrl = $_SERVER['REQUEST_URI'] ?? '/';
// var_dump($currentUrl);
// echo "<br>";
// echo'<pre>';
// var_dump($_SERVER);
// echo'</pre>';
if(strpos($currentUrl, '?') !== false)
{
$currentUrl = substr($currentUrl, 0, strpos($currentUrl, '?'));
}
// var_dump($currentUrl);
// echo "<br><br>";
$method = $_SERVER['REQUEST_METHOD'];
// var_dump($method);
if($method === 'GET')
{
//var_dump($currentUrl);
$fn = $this->getRoutes[$currentUrl] ?? null;
//var_dump($fn);
}
else
{ $fn = $this->postRoutes[$currentUrl] ?? null; }
// echo "<br>";
// var_dump($fn);
// echo "<br><br>";
// echo '<pre>';
// var_dump($this);
// echo "</pre>";
// $fn = $currentUrl;
if($fn)
{
// echo "chckpnt7<br><br>";
// echo '<pre>';
// var_dump($this);
// echo "</pre>";
// echo '<pre>';
// var_dump($fn);
// echo "</pre>";
// echo "chckpnt8<br><br>";
$that = $this;
call_user_func($fn, $that);
// $fn->{'index'}($this);
// $this->{'index'}($fn);
// $fn->{$index}($this);
// $this->{$index}($fn);
// $fn($this);
// $this($fn);
// $fn->{$this}();
// call_user_func($this, $fn);
// echo "chckpnt9<br><br>";
// echo '<pre>';
// var_dump($fn);
// echo "</pre>";
}
else { echo "Page Not Found"; }
}
public function renderView($view, $params = []) // products/index
{
foreach ($params as $key => $value)
{
$$key = $value;
}
// var_dump(__DIR__);
ob_start(); # To automatically send the content to the browser via local buffer
include_once __DIR__."/views/$view.php"; # The content that is being sent
$content = ob_get_clean(); # Cleaning the local buffer, value of the view html file in the $content
include_once __DIR__."/views/_layout.php";
// echo '<pre>';
// var_dump($_SERVER);
// echo '</pre>';
}
}