-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
63 lines (53 loc) · 1.04 KB
/
Router.php
File metadata and controls
63 lines (53 loc) · 1.04 KB
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
<?php
class Router
{
const FILE_LOAD_ERROR = "File not readable!";
private function __construct()
{
}
public static function init($routes)
{
$class = null;
$router = new Router();
$route = $router->getRoute();
$file = $router->getFile($routes, $route);
$class_name = ltrim(rtrim(strrchr($file, '/'),'.php'),'/');
//Include the requested file and initialise the class
$router->loadFile($file);
$class = $router->callClass($class_name);
$html = $class->printHtml();
if( $html != null )
{
echo $html;
}
}
private function getRoute()
{
if (isset($_SERVER["REQUEST_URI"]))
{
return $_SERVER["REQUEST_URI"];
}
return "/";
}
private function getFile( $routes, $route )
{
if( isset($routes[$route]) )
{
return $routes[$route];
}
return "Blog.php";
}
private function callClass($class_name)
{
return new $class_name;
}
private function loadFile($file)
{
$file_path = $file;
if (is_readable($file_path)) {
include($file_path);
}else{
return constant(Router::FILE_LOAD_ERROR);
}
}
}