48 lines
1.0 KiB
PHP
Executable File
48 lines
1.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\core;
|
|
use app\exceptions\PageNotFoundException;
|
|
|
|
class Router
|
|
{
|
|
public Request $request;
|
|
protected array $routes = [];
|
|
|
|
public function __construct(Request $request)
|
|
{
|
|
$this->request = $request;
|
|
}
|
|
|
|
public function get($path, $callback): void
|
|
{
|
|
$this->routes['GET'][$path] = $callback;
|
|
}
|
|
|
|
public function post($path, $callback): void
|
|
{
|
|
$this->routes['POST'][$path] = $callback;
|
|
}
|
|
|
|
/**
|
|
* @throws PageNotFoundException
|
|
*/
|
|
public function resolve()
|
|
{
|
|
$path = $this->request->getPath();
|
|
$method = $this->request->method();
|
|
$callback = $this->routes[$method][$path] ?? false;
|
|
|
|
if (!$callback) {
|
|
throw new PageNotFoundException();
|
|
}
|
|
|
|
Application::$app->controller = new $callback[0]();
|
|
|
|
/*
|
|
* TODO:
|
|
* de adaugat aici middleware urile asociate unui controller
|
|
*/
|
|
|
|
return call_user_func($callback, $this->request);
|
|
}
|
|
} |