-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
76 lines (55 loc) · 1.55 KB
/
index.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
<?php
// includeing config file
// if you change app directory
// please touch 'config.php' at the root
include('config.php');
function dispatcher($routes){
// Requested URL
$url = $_SERVER['REQUEST_URI'];
// Remove Application Address ROOT from URL
$url = str_replace('/'.APP_ROOT.'/','',$url);
// hold the name captured
$params = parse_params();
// removes query string from url we dont need any more that affects on $route
$url = str_replace('?'.$_SERVER['QUERY_STRING'],'',$url);
// become true if route['url'] matches url
$route_match = false;
// loops over routes looking for a match url
foreach($routes as $urls => $route)
{
// if match found append $matches to $params
// set $route_match to true and exit the loop.
if(preg_match($route['url'],$url,$matches))
{
$params = array_merge($params,$matches);
$route_match = true;
break;
}
}
// if no route found, display error
if(!$route_match){ exit('No Route Found'); };
// include controller
include(CONTROLLER_PATH.$route['controller'].'.php');
if(file_exists(VIEW_PATH.'layouts'.DS.$route['controller'].'.php')){
// include template ( layout )
include(VIEW_PATH.'layouts'.DS.$route['controller'].'.php');
}else{
include(VIEW_PATH.'layouts'.DS.'application'.'.php');
}
}
dispatcher($routes);
/**
* return array of $_POST and $_GET
* @return array
*/
function parse_params(){
$params = array();
if(!empty($_POST)){
$params = array_merge($params,$_POST);
}
if(!empty($_GET)){
$params = array_merge($params,$_GET);
}
return $params;
}
?>