From 9630f55da81686cd33150cbc1a346bf9bf57ec7b Mon Sep 17 00:00:00 2001
From: filippo.bertilotti <filippobertilotti@gmail.com>
Date: Tue, 28 May 2024 11:40:18 +0200
Subject: [PATCH] utilizzo dell'oggetto request nel controller e utilizzarlo al posto delle superglobals (parte 148)

---
 src/App/Controllers/products.php |   18 ++++++++++++++----
 1 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/App/Controllers/products.php b/src/App/Controllers/products.php
index 0929cef..3d211fe 100644
--- a/src/App/Controllers/products.php
+++ b/src/App/Controllers/products.php
@@ -2,8 +2,16 @@
 namespace App\Controllers;
 use App\Models\Product;
 use Framework\Exceptions\PageNotFoundException;
+use Framework\Request;
 use Framework\Viewer;
 class Products {
+
+    private Request $request;
+
+    public function setRequest(Request $request): void {
+        $this->request = $request;
+    }
+
     public function __construct(private Viewer $viewer, private Product $model) { }
     public function index() {
         
@@ -49,8 +57,8 @@
 
     public function create() {
         $data = [
-            "name" => $_POST["name"],
-            "description" => empty($_POST["description"]) ? null : $_POST["description"]
+            "name" => $this->request->post["name"],
+            "description" => empty($this->request->post["description"]) ? null : $this->request->post["description"]
         ];
         
         if($this->model->insert($data)) {
@@ -73,8 +81,8 @@
         $product = $this->getProduct($id);
         
 
-        $product["name"] = $_POST["name"];
-        $product["description"] = empty($_POST["description"]) ? null : $_POST["description"];
+        $product["name"] = $this->request->post["name"];
+        $product["description"] = empty($this->request->post["description"]) ? null : $this->request->post["description"];
         
         if($this->model->update($id, $product)) {
             header("Location: /products/{$id}/show");
@@ -125,4 +133,6 @@
         header("Location: /products/index");
         exit;
     }
+
+    
 }
\ No newline at end of file

--
Gitblit v1.8.0