From b62315aee0d26f98b1c1db083dae4ae73a110fad Mon Sep 17 00:00:00 2001
From: filippo.bertilotti <filippobertilotti@gmail.com>
Date: Tue, 07 May 2024 11:29:57 +0200
Subject: [PATCH] implementazione autoload delle class necessarie (parte 30)

---
 index.php |   30 +++++++++++++++++++++++++++---
 1 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/index.php b/index.php
index 5fb466a..4d33eda 100644
--- a/index.php
+++ b/index.php
@@ -1,5 +1,29 @@
 <?php
+spl_autoload_register(function ($class) {
+    require "src/$class.php";
+ });
 
-require "src/controllers/products.php";
-$controller = new Products;
-$controller->index();
\ No newline at end of file
+$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
+$segments = explode("/", $path);
+
+$router = new 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 = $params["controller"];
+$action = $params["action"];
+
+require "src/controllers/$controller.php";
+
+$controller_object = new $controller;
+
+$controller_object->$action();
+    
\ No newline at end of file

--
Gitblit v1.8.0