corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-14 ed1f744e1cf0045540c2e6349cc7dcc5031c8652
index.php
@@ -1,28 +1,24 @@
<?php
    $dns = "mysql:host=localhost;dbname=product_db;charset=utf8;port=3306";
    $pdo = new PDO($dns, "product_db_user", "secret", [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);
    $stmt = $pdo->query("SELECT * FROM product");
spl_autoload_register(function ($class) {
    require "src/". str_replace("\\", "/", $class). ".php";
 });
    $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
    print_r($products);
?>
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$segments = explode("/", $path);
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Products</title>
</head>
<body>
    <h1>Products</h1>
    <?php foreach ($products as $product): ?>
        <h2><?= htmlspecialchars($product["name"]) ?></h2>
        <p><?= htmlspecialchars($product["description"]) ?></p>
    <?php endforeach; ?>
</body>
</html>
$router = new Framework\Router;
$router->add("/admin/{controller}/{action}", ["namespace" => "Admin"]);
$router->add("/product/{slug:[\w-]+}", ["controller" => "products", "action" => "show"]);
$router->add("/{title}/{id:\d+}/{page:\d+}", ["controller" => "products", "action" => "showPage"]);
$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}");
$dispatcher = new Framework\Dispatcher($router);
$dispatcher->handle($path);