-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·57 lines (47 loc) · 1.54 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
<?php
/**
* Example use of router, all requests are redirected to index.php and serviced
*/
use router\Router;
use exceptions\ControllerNotFound;
use exceptions\ActionNotFoundException;
use exceptions\NotAuthenticatedException;
require_once __DIR__ . '/src/autoload.php';
define("CONTROLLER_NS", "Controller\\");
$router = new Router("/router");
// Controller = DefaultController, Action = doAction
$router->addRoute("/", 'default#do', [Router::GET]);
$matchAction = $router->match();
if (isset($matchAction)) {
$action_array = explode("#", $matchAction['action']);
$controller_str = CONTROLLER_NS . ucfirst($action_array[0]) . 'Controller';
$action_str = $action_array[1] . 'Action';
try {
if (class_exists($controller_str)) {
$controller = new $controller_str();
if (method_exists($controller, $action_str)) {
$controller->$action_str();
} else {
throw new ActionNotFoundException();
}
} else {
throw new ControllerNotFound();
}
} catch (ControllerNotFound $ex) {
http_response_code(500);
echo "Controller not found";
exit();
} catch (ActionNotFoundException $ex) {
http_response_code(500);
echo "Action not found";
exit();
} catch (NotAuthenticatedException $ex) {
http_response_code(403);
echo "Not authenticated";
exit();
}
} else {
http_response_code(404);
echo $_SERVER['REQUEST_URI'] . "<br/>";
echo "<h1>404 Not found</h1>";
}