-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrontController.php
73 lines (63 loc) · 2.25 KB
/
FrontController.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
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace Framework;
use Framework\Annotations\AnnotationsParser;
use Framework\Config\AppConfig;
use Framework\Exceptions\ApplicationException;
use Framework\Helpers\Helpers;
use Framework\HttpContext\HttpContext;
class FrontController
{
private $controllerName;
private $actionName;
private $requestParams = [];
private $controller;
private $router = null;
public function __construct(\Framework\Routers\Router $router){
$this->router = $router;
try{
$this->router->parseURI();
$this->controllerName = $router->getControllerName();
$this->actionName = $router->getActionName();
$this->requestParams = $router->getParams();
} catch (ApplicationException $e) {
$_SESSION["errors"] = $e->getMessage();
Helpers::redirect($e->getRedirectUrl());
} catch (\Exception $e){
Helpers::redirect("error");
}
}
public function dispatch(){
try {
$this->initController();
View::$controllerName = $this->controllerName;
View::$actionName = $this->actionName;
call_user_func_array(
[
$this->controller,
$this->actionName
],
$this->requestParams
);
} catch (ApplicationException $e) {
$_SESSION["errors"] = $e->getMessage();
Helpers::redirect("error");
} catch (\Exception $e) {
Helpers::redirect("error");
}
unset($_SESSION["errors"]);
$_SESSION["binding-errors"] = [];
}
private function initController(){
$controllerName = $this->controllerName;
if (!Helpers::startsWith($controllerName, AppConfig::CONTROLLERS_NAMESPACE)) {
$controllerName = AppConfig::CONTROLLERS_NAMESPACE
. ucfirst($this->controllerName)
. AppConfig::CONTROLLERS_SUFFIX;
}
class_exists($controllerName, false);
$annotationsParser = new AnnotationsParser($controllerName, $this->actionName);
$annotationsParser->checkAnnotations();
$this->controller = new $controllerName(HttpContext::getInstance());
}
}