diff --git a/add-product.php b/add-product.html
similarity index 100%
rename from add-product.php
rename to add-product.html
diff --git a/index.php b/index.php
index 29017af..e9b7df6 100644
--- a/index.php
+++ b/index.php
@@ -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();
diff --git a/src/Http/RequestHandler.php b/src/Http/RequestHandler.php
index cf61e6c..87b0244 100644
--- a/src/Http/RequestHandler.php
+++ b/src/Http/RequestHandler.php
@@ -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);
}
}
diff --git a/src/Http/Route.php b/src/Http/Route.php
index 38446f5..46fbf92 100644
--- a/src/Http/Route.php
+++ b/src/Http/Route.php
@@ -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)
{
- call_user_func($this->view, $request);
+ if (is_array($this->view)) {
+ call_user_func($this->view, $request);
+ } else {
+ $this->view->call($this);
+ }
}
}