Fix routing issues

This commit is contained in:
Augusto Gunsch 2022-07-27 20:59:48 +02:00
parent 8988bf02c0
commit 922b29519a
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
4 changed files with 10 additions and 12 deletions

View File

@ -9,9 +9,9 @@ $request = new Request($_SERVER);
$handler = new RequestHandler($request);
$handler->registerRoutes([
new Route('GET', 'products', ['ProductList\View\Product', 'listAll'])
new Route('GET', 'products', ['ProductList\View\Product', 'listAll']),
new Route('GET', 'add-product', function() { readfile('add-product.html'); }),
new Route('GET', '', function() { readfile('index.html'); }),
]);
$handler->setIndex('index.html');
$handler->handle();

View File

@ -3,7 +3,6 @@ namespace ProductList\Http;
class RequestHandler
{
private $index;
private $request;
private $routes;
@ -12,11 +11,6 @@ class RequestHandler
$this->request = $request;
}
public function setIndex($index)
{
$this->index = $index;
}
public function registerRoutes(array $routes)
{
$this->routes = $routes;
@ -31,6 +25,6 @@ class RequestHandler
}
}
readfile($this->index);
http_response_code(404);
}
}

View File

@ -7,7 +7,7 @@ class Route
private $uri;
private $view;
public function __construct(string $method, string $uri, array $view)
public function __construct(string $method, string $uri, array|\Closure $view)
{
$this->method = $method;
$this->uri = $uri;
@ -21,6 +21,10 @@ class Route
public function execute(Request $request)
{
if (is_array($this->view)) {
call_user_func($this->view, $request);
} else {
$this->view->call($this);
}
}
}