-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
57 lines (45 loc) · 1.57 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
<?php
session_start();
require_once('includes/config.php');
$requestParts = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
//Get default controller
$controllerName = DEFAULT_CONTROLLER;
//Get current controller name
if(count($requestParts) >= 2 && $requestParts[1] != ''){
$controllerName = $requestParts[1];
}
//Get default action
$action = DEFAULT_ACTION;
//Get current action
if(count($requestParts) >= 3 && $requestParts[2] != ''){
$action = $requestParts[2];
}
//Get params from URL
$params = array_splice($requestParts, 3);
//First letter of controller is uppercase
$controllerClassName = ucfirst(strtolower($controllerName)) . 'Controller';
$controllerFileName = "controllers/" . $controllerClassName . '.php';
//if controller exist create instance of controller
if(class_exists($controllerClassName)){
$controller = new $controllerClassName($controllerName, $action);
} else{
die("Cannot find controller '$controllerName' in class '$controllerFileName'");
}
//if action exist in current controller perform it
if(method_exists($controller, $action)){
call_user_func_array(array($controller, $action), $params);
$controller->renderView();
} else{
die("Cannot find action '$action' in controller '$controllerClassName'");
}
//Render view
$controller->renderView();
//Autoload method for controllers and methods
function __autoload($class_name){
if(file_exists("controllers/$class_name.php")){
include "controllers/$class_name.php";
}
if(file_exists("models/$class_name.php")){
include "models/$class_name.php";
}
}