From e2903b7b32aa672c9d821b0b18d26c20db5b00f4 Mon Sep 17 00:00:00 2001 From: filippo.bertilotti <filippobertilotti@gmail.com> Date: Fri, 10 May 2024 09:45:32 +0200 Subject: [PATCH] correzione regular expression per permettere di accettare tutti i caratteri tranne "/" (parte 58) --- index.php | 34 +++++++++++++++++++++++++++------- 1 files changed, 27 insertions(+), 7 deletions(-) diff --git a/index.php b/index.php index 98326de..dbfa428 100644 --- a/index.php +++ b/index.php @@ -1,13 +1,33 @@ <?php -require "src/controllers/products.php"; -$controller = new Products; -$action = $_GET["action"]; +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 Framework\Router; + +$router->add("/{controller}/{action}"); +$router->add("/{controller}/{id}/{action}"); +$router->add("/home/index", ["controller" => "home", "action" => "index"]); +$router->add("/products", ["controller" => "products", "action" => "index"]); +$router->add("/", ["controller" => "home", "action" => "index"]); -if($action === "index") { - $controller->index(); -}elseif($action === "show") { - $controller->show(); +$params = $router->match($path); + +print_r($params); + +if($params === false) { + exit("No routes matched"); } + +$controller = "App\Controllers\\" . ucwords($params["controller"]); +$action = $params["action"]; + +$controller_object = new $controller; + +$controller_object->$action(); \ No newline at end of file -- Gitblit v1.8.0