Files
FACULTATE-KNOW_YOUR_FOOD/knowyourfood/core/Router.php
T
2023-05-10 03:28:30 +03:00

50 lines
1.2 KiB
PHP

<?php
namespace app\core;
use app\core\exceptions\NotFoundException;
class Router
{
public Request $request;
public Response $response;
protected array $routes = [];
public function __construct(Request $request, Response $response)
{
$this->request = $request;
$this->response = $response;
}
public function get($path, $callback)
{
$this->routes['get'][$path] = $callback;
}
public function post($path, $callback)
{
$this->routes['post'][$path] = $callback;
}
public function resolve()
{
$path = $this->request->getPath();
$method = $this->request->method();
$callback = $this->routes[$method][$path] ?? false;
if (!$callback) {
throw new NotFoundException();
}
$controller = new $callback[0]();
Application::$app->controller = $controller;
$controller->action = $callback[1];
$callback[0] = $controller;
foreach ($controller->getMiddlewares() as $middleware) {
$middleware->execute();
}
return call_user_func($callback, $this->request, $this->response);
}
}