8 use FastRoute\Dispatcher
;
18 function __construct(BaseUrl
$baseUrl, Request
$request, Response
$response, Plates\Engine
$view) {
19 $this->baseUrl
= $baseUrl;
20 $this->request
= $request;
21 $this->response
= $response;
22 $this->view
= $view->addData(["location" => null]);
26 function __invoke(Dispatcher
$dispatcher) {
27 $route = $dispatcher->dispatch($this->request
->getRequestMethod(),
28 $this->baseUrl
->pathinfo($this->request
->getRequestUrl()));
31 case Dispatcher
::NOT_FOUND
:
32 $this->response
->setResponseCode(404);
33 $this->response
->getBody()->append($this->view
->render("404"));
36 case Dispatcher
::METHOD_NOT_ALLOWED
:
37 $this->response
->setResponseCode(405);
38 $this->response
->setHeader("Allowed", $route[1]);
39 $this->response
->getBody()->append($this->view
->render("405"));
42 case Dispatcher
::FOUND
:
43 list(, $handler, $args) = $route;
44 $handler(array_map("urldecode", $args));
48 $this->response
->send();
51 function display($view, array $data = []) {
52 $this->response
->getBody()->append(
53 $this->view
->render($view, $data));
56 function redirect($url, $code = 302) {
57 $this->response
->setResponseCode($code);
58 $this->response
->setHeader("Location", $url);
61 function getBaseUrl() {
62 return $this->baseUrl
;
69 function getRequest() {
70 return $this->request
;
73 function getResponse() {
74 return $this->response
;