-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRoute.php
79 lines (60 loc) · 2.08 KB
/
Route.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
<?php
class Route {
static $status;
static function splitUri( $uri = null ) {
if( is_null($uri) ) {
$cutGet = explode("?", $_SERVER['REQUEST_URI'] );
$uri = $cutGet[0];
}
$temp = explode("/", $uri );
if ( count($temp)>1 ) {
unset($temp[0]);
$temp = array_values($temp);
}
return $temp;
}
static function get( $uri , $controller , $vars = [] ) {
if( Route::$status ) { return false; }
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) { return false; }
Route::run( $uri , $controller , $vars );
}
static function post( $uri , $controller , $vars = [] ) {
if( Route::$status ) { return false; }
if( $_SERVER['REQUEST_METHOD'] != 'POST' ) { return false; }
Route::run( $uri , $controller , $vars );
}
static private function run( $uri , $controller , $vars = [] ) {
$sltCt = Route::splitUri();
$slt = Route::splitUri($uri);
if( count($slt)!=count($sltCt) ) {
return false;
}
for( $i=0; $i<count( $sltCt ); $i++ ) {
if( preg_match("/^{+.+}$/", $slt[$i]) ) {
$temp = substr( $slt[$i], 1, -1);
if( isset($vars[$temp]) && preg_match($vars[$temp], $sltCt[$i]) ) {
$vars[$temp] = $sltCt[$i];
} else {
return false;
}
} else if( $slt[$i]!=$sltCt[$i] ) {
return false;
}
}
Route::$status = Route::call($controller , $vars);
}
static function call( $controller , $req ) {
$conthodler = explode("@", $controller);
require_once( ROOT_CONTROLLER . $conthodler[0] .".php" );
$controller = new $conthodler[0]();
if( count($req)==0 )
$controller->{ $conthodler[1] }();
else
$controller->{ $conthodler[1] }($req);
return true;
}
static function error() {
if( Route::$status ) { return false; }
echo "404 Page Not Found.";
}
}