-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·61 lines (39 loc) · 1.26 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
58
59
60
61
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
require __DIR__ . '/src/helpers.php';
require __DIR__ . '/vendor/autoload.php'; //композер
function myAutoload($class){
require __DIR__ . '/src/' . str_replace('\\', '/', $class) . '.php' ;
}
try{
spl_autoload_register('myAutoload');
$view = new MyProject\Views\View; //для DI
$db = new MyProject\Services\Db;
$routes = require __DIR__ . '/src/routes.php';
$currentRoute = $_SERVER['REQUEST_URI'];
$routeFound = false;
foreach($routes as $url => $classAndMethod){
preg_match($url, $currentRoute, $matches);
if(!empty($matches)){
$routeFound = true;
break;
}
}
if(!$routeFound) {
throw new \Exception('Страница не найдена.');
return;
}
unset($matches[0]);
$controllerName = $classAndMethod[0];
$classMethod = $classAndMethod[1];
$controller = new $controllerName($view, $db);
echo $controller->$classMethod(...$matches);
}
catch (\MyProject\Exceptions\UnauthorizedException $e) {
$view->render('/errors/401.php', ['error' => $e->getMessage()], 401);
}
catch (\Exception $e) {
echo $e->getMessage();
}
//var_dump(\MyProject\Services\Db::getInstancesCount()); количество подключений к базе