-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRouter.php
executable file
·67 lines (51 loc) · 1.67 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
<?php
declare(strict_types=1);
class Router
{
private $routes;
public function __construct()
{
}
function addRoute($name,$controller,$method,$auth=array(0,1,2,3,4,5))
{
if(isset($this->routes[$name]))
{
array_push($this->routes[$name], ["controller"=>$controller,"method"=>$method,"authorization"=>$auth]);
}else{
$this->routes[$name] = ["controller"=>$controller,"method"=>$method,"authorization"=>$auth];
}
}
function routePage($name,$params)
{
$routes = $this->routes;
if(isset($routes[$name]))
{
$authorized = FALSE;
foreach ($routes[$name]["authorization"] as $key) {
if($_SESSION["authorization"]==$key)
{
$authorized = TRUE;
}
}
if($authorized == FALSE)
{
header('HTTP/1.0 403 Forbidden');
echo "<h1> 403 Forbidden </h1>";
exit();
}else{
$match=$routes[$name];
call_user_func_array(array(new $match["controller"], $match["method"]), $params);
$index = 0;
while(isset($match[$index]))
{
call_user_func_array(array(new $match[$index]["controller"], $match[$index]["method"]), $params);
$index++;
}
}
}else{
header('HTTP/1.0 404 Not Found');
echo "<h1> 404 Not Found </h1>";
exit();
}
}
}