-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
49 lines (48 loc) · 1.08 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
<?php
//front crontroller
function page_not_found(){
echo "404\n";
}
//echo "index.php\n";
$base_path='/var/www/html/piggy_assesment/';
$base_uri='/piggy_assesment';
$uri_raw = $_SERVER['REQUEST_URI'];
$uri = preg_replace(":^$base_uri:", "", $uri_raw);
//echo '$uri'."\n";
//var_dump($uri);
//we want to split on slashes
$parts_raw = explode('/', $uri);
$parts = array_slice($parts_raw, 1);
//echo "\$parts\n";
//var_dump($parts);
if(count($parts)>0&&$parts[0]!="") {
$controller_name = $parts[0];
}
else{
$controller_name = "DefaultController";
}
if(count($parts)>1){
$function_name = $parts[1];
}
$params = array_slice($parts, 2);
$controller_path = "$base_path/controllers/$controller_name.php";
//echo "\$controller_path\n";
//echo "";
if(file_exists($controller_path)){
require($controller_path);
$controller = new $controller_name;
if(!isset($function_name)) {
//default method
$function_name = "index";
}
if(method_exists($controller, $function_name)) {
call_user_func_array([$controller, $function_name], $params);
}
else{
page_not_found();
}
}
else{
page_not_found();
}
?>