diff --git a/index.js b/index.js index aec870e..5c70a1e 100644 --- a/index.js +++ b/index.js @@ -4,8 +4,6 @@ const loadItems = () => { const xhttp = new XMLHttpRequest(); xhttp.onload = function() { - console.log(this.responseText); - const products = JSON.parse(this.responseText); const boxes = products.map(product => @@ -23,7 +21,7 @@ const loadItems = () => { productsList.innerHTML = boxes.join('\n'); } - xhttp.open('GET', 'src/View/Product', true); + xhttp.open('GET', 'products', true); xhttp.send(); } loadItems(); @@ -39,7 +37,7 @@ const deleteSelected = () => { loadItems(); } - xhttp.open('DELETE', 'src/View/Product', true); + xhttp.open('DELETE', 'products', true); xhttp.send(); } diff --git a/index.php b/index.php index 7ed317b..29017af 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,17 @@ registerRoutes([ + new Route('GET', 'products', ['ProductList\View\Product', 'listAll']) +]); + +$handler->setIndex('index.html'); + +$handler->handle(); diff --git a/src/Http/Request.php b/src/Http/Request.php new file mode 100644 index 0000000..59fd43a --- /dev/null +++ b/src/Http/Request.php @@ -0,0 +1,24 @@ +method; + } + + public function getUri() + { + return $this->uri; + } + + public function __construct(array $params) + { + $this->uri = basename($params['REQUEST_URI']); + $this->method = $params['REQUEST_METHOD']; + } +} diff --git a/src/Http/RequestHandler.php b/src/Http/RequestHandler.php new file mode 100644 index 0000000..cf61e6c --- /dev/null +++ b/src/Http/RequestHandler.php @@ -0,0 +1,36 @@ +request = $request; + } + + public function setIndex($index) + { + $this->index = $index; + } + + public function registerRoutes(array $routes) + { + $this->routes = $routes; + } + + public function handle() + { + foreach ($this->routes as $route) { + if ($route->matches($this->request)) { + $route->execute($this->request); + return; + } + } + + readfile($this->index); + } +} diff --git a/src/Http/Route.php b/src/Http/Route.php new file mode 100644 index 0000000..38446f5 --- /dev/null +++ b/src/Http/Route.php @@ -0,0 +1,26 @@ +method = $method; + $this->uri = $uri; + $this->view = $view; + } + + public function matches(Request $request) + { + return $this->method === $request->getMethod() && $this->uri === $request->getUri(); + } + + public function execute(Request $request) + { + call_user_func($this->view, $request); + } +} diff --git a/src/View/Product.php b/src/View/Product.php index de0c3f2..03867e0 100644 --- a/src/View/Product.php +++ b/src/View/Product.php @@ -1,4 +1,13 @@