corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-21 ce9b2119ceb911faab15fa43e741d63e3fb7834c
aggiunto ref a ogni product e aggiunta eccezione di prodotto non trovato in caso non esista nel database (parte 119)
2 files modified
13 ■■■■ changed files
src/App/Controllers/products.php 6 ●●●● patch | view | raw | blame | history
views/Products/index.php 7 ●●●● patch | view | raw | blame | history
src/App/Controllers/products.php
@@ -1,12 +1,13 @@
<?php
namespace App\Controllers;
use App\Models\Product;
use Framework\Exceptions\PageNotFoundException;
use Framework\Viewer;
class Products {
    public function __construct(private Viewer $viewer, private Product $model) { }
    public function index() {
        
        $products = $this->model->getData();
        $products = $this->model->findAll();
        echo $this->viewer->render("shared/header.php");
        echo $this->viewer->render("Products/index.php", [
@@ -16,6 +17,9 @@
    public function show(string $id) {
        $product = $this->model->find($id);
        if( $product == false ) {
            throw new PageNotFoundException("Product not found");
        }
        echo $this->viewer->render("shared/header.php");
        echo $this->viewer->render("Products/show.php", [
            "product"=> $product
views/Products/index.php
@@ -1,8 +1,11 @@
<body>
    <h1>Products</h1>
    <?php foreach ($products as $product): ?>
        <h2><?= htmlspecialchars($product["name"]) ?></h2>
        <p><?= htmlspecialchars($product["description"]) ?></p>
        <h2>
            <a href="/products/<?= $product["id"] ?>/show">
                <?= htmlspecialchars($product["name"]) ?>
            </a>
        </h2>
    <?php endforeach; ?>
</body>
</html>