-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
47 lines (41 loc) · 1.78 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
<?php
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
ini_set('display_errors', 1);
try {
spl_autoload_register(function (string $className) {
require_once __DIR__ . '/src/' . str_replace('\\', '/', $className) . '.php';
});
$route = $_GET['route'] ?? '';
$routes = require __DIR__ . '/src/routes.php';
$isRouteFound = false;
foreach ($routes as $pattern => $controllerAndAction) {
preg_match($pattern, $route, $matches);
if (!empty($matches)) {
$isRouteFound = true;
break;
}
}
if (!$isRouteFound) {
throw new \MyProject\Exceptions\NotFoundException();
}
unset($matches[0]);
$controllerName = $controllerAndAction[0];
$actionName = $controllerAndAction[1];
$controller = new $controllerName();
$controller->$actionName(...$matches);
} catch (\MyProject\Exceptions\DbException $e) {
$view = new \MyProject\View\View(__DIR__ . '/templates/errors');
$view->renderHtml('500.php', ['error' => $e->getMessage()], 500);
} catch (\MyProject\Exceptions\NotFoundException $e) {
$view = new \MyProject\View\View(__DIR__ . '/templates/errors');
$view->renderHtml('404.php', ['error' => $e->getMessage()], 404);
} catch (\MyProject\Exceptions\UnauthorizedException $e) {
$view = new \MyProject\View\View(__DIR__ . '/templates/errors');
$view->renderHtml('401.php', ['error' => $e->getMessage()], 401);
} catch (\MyProject\Exceptions\Forbidden $e) {
$view = new \MyProject\View\View(__DIR__ . '/templates/errors');
$view->renderHtml('403.php', ['error' => $e->getMessage()], 403);
} catch (\MyProject\Exceptions\IsNotAdmin $e) {
$view = new \MyProject\View\View(__DIR__ . '/templates/errors');
$view->renderHtml('404.php', ['error' => $e->getMessage()], 404);
}