From 5cc38b583e04398004e33bd09447c91cb3c618cb Mon Sep 17 00:00:00 2001
From: filippo.bertilotti <filippobertilotti@gmail.com>
Date: Wed, 08 May 2024 09:46:25 +0200
Subject: [PATCH] aggiornamento codice: implementazione comando "use" e refactor

---
 index.php |   33 ++++++++++++++++++++-------------
 1 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/index.php b/index.php
index 1da17f5..6a9577d 100644
--- a/index.php
+++ b/index.php
@@ -1,21 +1,28 @@
 <?php
 
-$action = $_GET["action"];
-$controller = $_GET["controller"];
+spl_autoload_register(function ($class) {
+    require "src/". str_replace("\\", "/", $class). ".php";
+ });
 
-if($controller === "products") {
-    require "src/controllers/products.php";
-    $controller_object = new Products;
-}elseif($controller === "home") {
-    require "src/controllers/home.php";
-    $controller_object = new Home;
+$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
+$segments = explode("/", $path);
 
+$router = new Framework\Router;
+$router->add("/home/index", ["controller" => "home", "action" => "index"]);
+$router->add("/products", ["controller" => "products", "action" => "index"]);
+$router->add("/", ["controller" => "home", "action" => "index"]);
+
+
+$params = $router->match($path);
+
+if($params === false) {
+    exit("No routes matched");
 }
 
+$controller = "App\Controllers\\" . ucwords($params["controller"]);
+$action = $params["action"];
 
-if($action === "index") {
-    $controller_object->index();
-}elseif($action === "show") {
-    $controller_object->show();
-}
+$controller_object = new $controller;
+
+$controller_object->$action();
     
\ No newline at end of file

--
Gitblit v1.8.0