Sfoglia il codice sorgente

created Request class to get data from request

Плотников Роман Вячеславович 3 anni fa
parent
commit
d9b79c16eb
3 ha cambiato i file con 42 aggiunte e 3 eliminazioni
  1. 3 2
      app/Controller/Site.php
  2. 38 0
      core/Src/Request.php
  3. 1 1
      core/Src/Route.php

+ 3 - 2
app/Controller/Site.php

@@ -3,14 +3,15 @@
 namespace Controller;
 
 use Src\View;
+use Src\Request;
 use Illuminate\Database\Capsule\Manager as DB;
 use Model\Post;
 
 class Site
 {
-    public function index(): string
+    public function index(Request $request): string
     {
-        $posts = Post::all();
+        $posts = Post::where('id', $request->id)->get();
         return (new View())->render('site.post', ['posts' => $posts]);
     }
 

+ 38 - 0
core/Src/Request.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace Src;
+
+use Error;
+
+class Request {
+    protected array $body;
+    public string $method;
+    public array $headers;
+
+    public function __construct()
+    {
+        $this->body = $_REQUEST;
+        $this->method = $_SERVER['REQUEST_METHOD'];
+        $this->headers = getallheaders() ?? [];
+    }
+
+    public function set($field, $value): void {
+        $this->body[$field] = $value;
+    }
+
+    public function get($field) {
+        return $this->body[$field];
+    }
+
+    public function files(): array {
+        return $_FILES;
+    }
+
+    public function __get($key)
+    {
+        if (array_key_exists($key, $this->body)) {
+            return $this->body[$key];
+        }
+        throw new Error('Acessing a non-existent property');
+    }
+}

+ 1 - 1
core/Src/Route.php

@@ -41,6 +41,6 @@ class Route
             throw new Error('Method does not exist');
         }
 
-        call_user_func([new $class, $action]);
+        call_user_func([new $class, $action], new Request());
     }
 }