From f6df29226eaad3c37245b8774de6639acf589b1d Mon Sep 17 00:00:00 2001
From: filippo.bertilotti <filippobertilotti@gmail.com>
Date: Fri, 10 May 2024 11:23:50 +0200
Subject: [PATCH] refactor code index dispatcher (parte 64)

---
 index.php |   21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/index.php b/index.php
index 2e99c7b..9ebc44a 100644
--- a/index.php
+++ b/index.php
@@ -1,17 +1,22 @@
 <?php
-require "src/router.php";
+
+spl_autoload_register(function ($class) {
+    require "src/". str_replace("\\", "/", $class). ".php";
+ });
 
 $path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
 $segments = explode("/", $path);
 
-$router = new Router;
+$router = new Framework\Router;
 
-$controller = $segments[1];
-$action = $segments[2];
+$router->add("/product/{slug:[\w-]+}", ["controller" => "products", "action" => "show"]);
+$router->add("/{controller}/{id:\d+}/{action}");
+$router->add("/home/index", ["controller" => "home", "action" => "index"]);
+$router->add("/products", ["controller" => "products", "action" => "index"]);
+$router->add("/", ["controller" => "home", "action" => "index"]);
+$router->add("/{controller}/{action}");
 
-require "src/controllers/$controller.php";
+$dispatcher = new Framework\Dispatcher($router);
 
-$controller_object = new $controller;
-
-$controller_object->$action();
+$dispatcher->handle($path);
     
\ No newline at end of file

--
Gitblit v1.8.0