forked from artsofte-php-course/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
51 lines (36 loc) · 1.32 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
<?php
require_once 'core/Request.php';
require_once 'core/Response.php';
require_once 'core/Router.php';
require_once 'core/BaseController.php';
require_once 'validator/ArticleValidator.php';
require_once 'repositories/ArticleRepository.php';
require_once 'controllers/IndexController.php';
require_once 'controllers/HelloWorldController.php';
include_once 'config/routes.php';
include_once 'config/database.php';
$router = new Router($routes);
$request = Request::createFromGlobals();
$dsn = sprintf("mysql:host=%s;dbname=%s;charset=%s", $database['database_host'], $database['database_name'], $database['charset']);
/** @var PDO $connection */
$connection = new PDO( $dsn, $database['username'], $database['password'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$articleRepository = new ArticleRepository($connection);
try {
$route = $router->match($request->getPath());
} catch (\InvalidArgumentException $exception) {
$route = [
'controller' => 'index',
'action' => 'index'
];
}
$controllers = [
'index' => new IndexController($articleRepository),
'helloWorld' => new HelloWorldController(),
];
$controller = $controllers[$route['controller']];
$actionMethod = $route['action'] . 'Action';
/** @var Response $response */
$response = $controller->$actionMethod($request);
$response->send();