]> git.leonardobizzoni.com Git - highschool-graduation-project/commitdiff
Views template
authorLeonardoBizzoni <leo2002714@gmail.com>
Thu, 14 Apr 2022 10:11:18 +0000 (12:11 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Thu, 14 Apr 2022 10:11:18 +0000 (12:11 +0200)
README.org
dockerfiles/web/nginx.conf
www/.gitignore [new file with mode: 0644]
www/core/Application.php
www/core/Response.php [new file with mode: 0644]
www/core/Router.php
www/pub/index.php
www/views/404.php [new file with mode: 0644]
www/views/contact.php [new file with mode: 0644]
www/views/home.php [new file with mode: 0644]
www/views/layouts/main.php [new file with mode: 0644]

index 9f3efb39f48e0448a28aff05fe2dd4872de409e5..c2bf2a9491f68c280e8c43b8474840158e62276c 100644 (file)
@@ -11,6 +11,7 @@
 - [[#realizzazione][Realizzazione]]
   - [[#entry-point---indexphp][Entry point - index.php]]
   - [[#core][Core]]
+  - [[#views][Views]]
 
 * Descrizione del progetto
 Su [questo-sito] è possibile registrare un account con cui si può tenere traccia di:
@@ -105,18 +106,19 @@ http {
                server_name _;
 
                location / {
-                        try_files $uri $uri/ =404;
-                }
-
-                location ~ \.php$ {
-                        fastcgi_pass      127.0.0.1:9000;
-                        fastcgi_index     index.php;
-                        include           fastcgi.conf;
-                }
+                    try_files $uri $uri/ /index.php?$args;
+               }
+
+               location ~ \.php$ {
+                    fastcgi_pass      127.0.0.1:9000;
+                    fastcgi_index     index.php;
+                    include           fastcgi.conf;
+               }
         }
 }
 #+end_src
 
+
 *** Server database - MariaDB
 L'installazione del database server mariadb viene anch'essa eseguita tramite un docker container e la container image disponibile su [[https://hub.docker.com/_/mariadb][hub.docker]] per cui non ha bisogno di alcuna configurazione.
 
@@ -141,13 +143,13 @@ La home page, si occupa di inizializzare l'applicazione ed impostare le route co
 require_once __DIR__."/../vendor/autoload.php";
 use app\core\Application;
 
-$app = new Application();
+$app = new Application(dirname(__DIR__));
 
-$app->router->get("/", function() {
-    return "Hello World!";
-});
-$app->router->get("/contact", function() {
-    return "Contact me!";
+$app->router->get("/", "home");
+$app->router->get("/contact", "contact");
+
+$app->router->post("/contact", function() {
+    echo "handling submitted data";
 });
 
 $app->run();
@@ -165,14 +167,22 @@ namespace app\core;
 class Application {
     public Router $router;
     public Request $req;
+    public Response $res;
+
+    public static Application $app;
+    public static string $ROOT_DIR;
+
+    public function __construct(string $root) {
+        self::$ROOT_DIR = $root;
+        self::$app = $this;
 
-    public function __construct() {
         $this->req = new Request();
-        $this->router = new Router($this->req);
+        $this->res = new Response();
+        $this->router = new Router($this->req, $this->res);
     }
 
     public function run() {
-        $this->router->resolve();
+        echo $this->router->resolve();
     }
 }
 ?>
@@ -208,16 +218,20 @@ Metodi:
 
 #+begin_src php :tangle www/core/Router.php
 <?php
+
 namespace app\core;
 
 class Router
 {
-    public Request $req;
     private array $routes = [];
 
-    public function __construct(Request $req)
+    public Request $req;
+    public Response $res;
+
+    public function __construct(Request $req, Response $res)
     {
         $this->req = $req;
+        $this->res = $res;
     }
 
     public function get($path, $callback)
@@ -237,12 +251,35 @@ class Router
         $method = $this->req->getMethod();
         $callback = $this->routes[$method][$path] ?? false;
 
-        if ($callback) {
-            echo call_user_func($callback);
+        if (is_string($callback)) {
+            echo $this->renderView($callback);
+        } else if ($callback) {
+            return call_user_func($callback);
         } else {
-            echo "Not found!";
+            $this->res->setStatusCode(404);
+            echo $this->renderView("404");
         }
     }
+
+    public function renderView(string $view)
+    {
+        $layoutContent = $this->loadLayoutContent();
+        $viewContent = $this->loadViewContent($view);
+
+        return str_replace("{{content}}", $viewContent, $layoutContent);
+    }
+
+    private function loadLayoutContent() {
+        ob_start();
+        include_once Application::$ROOT_DIR."/views/layouts/main.php";
+        return ob_get_clean();
+    }
+
+    private function loadViewContent(string $view) {
+        ob_start();
+        include_once Application::$ROOT_DIR."/views/$view.php";
+        return ob_get_clean();
+    }
 }
 ?>
 #+end_src
@@ -279,3 +316,51 @@ class Request {
 }
 ?>
 #+end_src
+
+*** Response class
+#+begin_src php :tangle www/core/Response.php
+<?php
+namespace app\core;
+
+class Response {
+    public function setStatusCode(int $code) {
+        http_response_code($code);
+    }
+}
+?>
+#+end_src
+
+** Views
+*** Home
+#+begin_src php :tangle www/views/home.php
+<h1>Hello, World!</h1>
+#+end_src
+
+*** Contact
+#+begin_src php :tangle www/views/contact.php
+<h1>Contact page</h1>
+
+    <div class="container">
+<form action="" method="post">
+  <div class="mb-3">
+    <label for="exampleInputEmail1" class="form-label">Email address</label>
+    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
+    <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
+  </div>
+  <div class="mb-3">
+    <label for="exampleInputPassword1" class="form-label">Password</label>
+    <input type="password" class="form-control" id="exampleInputPassword1">
+  </div>
+  <div class="mb-3 form-check">
+    <input type="checkbox" class="form-check-input" id="exampleCheck1">
+    <label class="form-check-label" for="exampleCheck1">Check me out</label>
+  </div>
+  <button type="submit" class="btn btn-primary">Submit</button>
+</form>
+</div>
+#+end_src
+
+*** 404
+#+begin_src php :tangle www/views/404.php
+<h1>404 - File not found!</h1>
+#+end_src
index 8fe9f980181a4c57025a7b0048da4daf81582be2..2775a51312d054b4532cd4e3a4f416607d47ba6b 100644 (file)
@@ -17,13 +17,13 @@ http {
                server_name _;
 
                location / {
-                        try_files $uri $uri/ =404;
-                }
+                    try_files $uri $uri/ /index.php?$args;
+               }
 
-                location ~ \.php$ {
-                        fastcgi_pass      127.0.0.1:9000;
-                        fastcgi_index     index.php;
-                        include           fastcgi.conf;
-                }
+               location ~ \.php$ {
+                    fastcgi_pass      127.0.0.1:9000;
+                    fastcgi_index     index.php;
+                    include           fastcgi.conf;
+               }
         }
 }
diff --git a/www/.gitignore b/www/.gitignore
new file mode 100644 (file)
index 0000000..57872d0
--- /dev/null
@@ -0,0 +1 @@
+/vendor/
index 21f10633d54100d62b7fbf503b497d923c5ebbb2..e5567c0605e027d3c447d144349489472fc232fc 100644 (file)
@@ -4,14 +4,22 @@ namespace app\core;
 class Application {
     public Router $router;
     public Request $req;
+    public Response $res;
+
+    public static Application $app;
+    public static string $ROOT_DIR;
+
+    public function __construct(string $root) {
+        self::$ROOT_DIR = $root;
+        self::$app = $this;
 
-    public function __construct() {
         $this->req = new Request();
-        $this->router = new Router($this->req);
+        $this->res = new Response();
+        $this->router = new Router($this->req, $this->res);
     }
 
     public function run() {
-        $this->router->resolve();
+        echo $this->router->resolve();
     }
 }
 ?>
diff --git a/www/core/Response.php b/www/core/Response.php
new file mode 100644 (file)
index 0000000..06c2295
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+namespace app\core;
+
+class Response {
+    public function setStatusCode(int $code) {
+        http_response_code($code);
+    }
+}
+?>
index ccd884ca8b930333175edc5afd24790d38aa1618..6589ac4c4098535795d376694f3c92eedf57efa6 100644 (file)
@@ -1,14 +1,18 @@
 <?php
+
 namespace app\core;
 
 class Router
 {
-    public Request $req;
     private array $routes = [];
 
-    public function __construct(Request $req)
+    public Request $req;
+    public Response $res;
+
+    public function __construct(Request $req, Response $res)
     {
         $this->req = $req;
+        $this->res = $res;
     }
 
     public function get($path, $callback)
@@ -16,6 +20,11 @@ class Router
         $this->routes["get"][$path] = $callback;
     }
 
+    public function post($path, $callback)
+    {
+        $this->routes["post"][$path] = $callback;
+    }
+
 
     public function resolve()
     {
@@ -23,11 +32,34 @@ class Router
         $method = $this->req->getMethod();
         $callback = $this->routes[$method][$path] ?? false;
 
-        if (!$callback) {
-            echo "Not found!";
+        if (is_string($callback)) {
+            echo $this->renderView($callback);
+        } else if ($callback) {
+            return call_user_func($callback);
         } else {
-            echo call_user_func($callback);
+            $this->res->setStatusCode(404);
+            echo $this->renderView("404");
         }
     }
+
+    public function renderView(string $view)
+    {
+        $layoutContent = $this->loadLayoutContent();
+        $viewContent = $this->loadViewContent($view);
+
+        return str_replace("{{content}}", $viewContent, $layoutContent);
+    }
+
+    private function loadLayoutContent() {
+        ob_start();
+        include_once Application::$ROOT_DIR."/views/layouts/main.php";
+        return ob_get_clean();
+    }
+
+    private function loadViewContent(string $view) {
+        ob_start();
+        include_once Application::$ROOT_DIR."/views/$view.php";
+        return ob_get_clean();
+    }
 }
 ?>
index 6efca35d0f3761545a268574173dc327fe43df1d..5c05d91c5031f830b0d6176acd15a4de801a5e56 100644 (file)
@@ -2,13 +2,13 @@
 require_once __DIR__."/../vendor/autoload.php";
 use app\core\Application;
 
-$app = new Application();
+$app = new Application(dirname(__DIR__));
 
-$app->router->get("/", function() {
-    return "Hello World!";
-});
-$app->router->get("/contact", function() {
-    return "Contact me!";
+$app->router->get("/", "home");
+$app->router->get("/contact", "contact");
+
+$app->router->post("/contact", function() {
+    echo "handling submitted data";
 });
 
 $app->run();
diff --git a/www/views/404.php b/www/views/404.php
new file mode 100644 (file)
index 0000000..90593e5
--- /dev/null
@@ -0,0 +1 @@
+<h1>404 - File not found!</h1>
diff --git a/www/views/contact.php b/www/views/contact.php
new file mode 100644 (file)
index 0000000..d1c4d6c
--- /dev/null
@@ -0,0 +1,20 @@
+<h1>Contact page</h1>
+
+    <div class="container">
+<form action="" method="post">
+  <div class="mb-3">
+    <label for="exampleInputEmail1" class="form-label">Email address</label>
+    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
+    <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
+  </div>
+  <div class="mb-3">
+    <label for="exampleInputPassword1" class="form-label">Password</label>
+    <input type="password" class="form-control" id="exampleInputPassword1">
+  </div>
+  <div class="mb-3 form-check">
+    <input type="checkbox" class="form-check-input" id="exampleCheck1">
+    <label class="form-check-label" for="exampleCheck1">Check me out</label>
+  </div>
+  <button type="submit" class="btn btn-primary">Submit</button>
+</form>
+</div>
diff --git a/www/views/home.php b/www/views/home.php
new file mode 100644 (file)
index 0000000..f76d8e9
--- /dev/null
@@ -0,0 +1 @@
+<h1>Hello, World!</h1>
diff --git a/www/views/layouts/main.php b/www/views/layouts/main.php
new file mode 100644 (file)
index 0000000..b394359
--- /dev/null
@@ -0,0 +1,44 @@
+<!doctype html>
+<html lang="en">
+
+<head>
+    <meta charset="UTF-8" />
+    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
+
+    <title>Document</title>
+</head>
+
+<body>
+    <nav class="navbar navbar-expand-lg navbar-light bg-light">
+        <div class="container-fluid">
+            <a class="navbar-brand" href="#">Navbar</a>
+            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
+                <span class="navbar-toggler-icon"></span>
+            </button>
+            <div class="collapse navbar-collapse" id="navbarNav">
+                <ul class="navbar-nav">
+                    <li class="nav-item">
+                        <a class="nav-link active" aria-current="page" href="#">Home</a>
+                    </li>
+                    <li class="nav-item">
+                        <a class="nav-link" href="#">Features</a>
+                    </li>
+                    <li class="nav-item">
+                        <a class="nav-link" href="#">Pricing</a>
+                    </li>
+                    <li class="nav-item">
+                        <a class="nav-link disabled">Disabled</a>
+                    </li>
+                </ul>
+            </div>
+        </div>
+    </nav>
+
+    <div class="container">
+        {{content}}
+    </div>
+
+    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
+</body>
+
+</html>