Fix routing issues
This commit is contained in:
parent
8988bf02c0
commit
922b29519a
|
@ -9,9 +9,9 @@ $request = new Request($_SERVER);
|
||||||
$handler = new RequestHandler($request);
|
$handler = new RequestHandler($request);
|
||||||
|
|
||||||
$handler->registerRoutes([
|
$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();
|
$handler->handle();
|
||||||
|
|
|
@ -3,7 +3,6 @@ namespace ProductList\Http;
|
||||||
|
|
||||||
class RequestHandler
|
class RequestHandler
|
||||||
{
|
{
|
||||||
private $index;
|
|
||||||
private $request;
|
private $request;
|
||||||
private $routes;
|
private $routes;
|
||||||
|
|
||||||
|
@ -12,11 +11,6 @@ class RequestHandler
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setIndex($index)
|
|
||||||
{
|
|
||||||
$this->index = $index;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function registerRoutes(array $routes)
|
public function registerRoutes(array $routes)
|
||||||
{
|
{
|
||||||
$this->routes = $routes;
|
$this->routes = $routes;
|
||||||
|
@ -31,6 +25,6 @@ class RequestHandler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
readfile($this->index);
|
http_response_code(404);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ class Route
|
||||||
private $uri;
|
private $uri;
|
||||||
private $view;
|
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->method = $method;
|
||||||
$this->uri = $uri;
|
$this->uri = $uri;
|
||||||
|
@ -21,6 +21,10 @@ class Route
|
||||||
|
|
||||||
public function execute(Request $request)
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue