54 lines
1.2 KiB
PHP
Executable File
54 lines
1.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Core;
|
|
|
|
class Router
|
|
{
|
|
protected array $routes = [];
|
|
public Request $request;
|
|
public Response $response;
|
|
|
|
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->getMethod();
|
|
$callback = $this->routes[$method][$path] ?? false;
|
|
|
|
if ($callback === false) {
|
|
$this->response->setStatusCode(404);
|
|
return $this->renderView("_404");
|
|
}
|
|
|
|
if (is_string($callback)) {
|
|
return $this->renderView($callback);
|
|
}
|
|
|
|
if (is_array($callback)) {
|
|
$callback[0] = new $callback[0]();
|
|
}
|
|
|
|
return call_user_func($callback, $this->request);
|
|
}
|
|
|
|
public function renderView($view, $params = [])
|
|
{
|
|
return Application::$app->view->renderView($view, $params);
|
|
}
|
|
}
|