From f9ec67aff97a1b93b0d05acdc22ff3f98e1779e4 Mon Sep 17 00:00:00 2001 From: LeonardoBizzoni Date: Thu, 14 Apr 2022 12:11:18 +0200 Subject: [PATCH] Views template --- README.org | 129 ++++++++++++++++++++++++++++++------- dockerfiles/web/nginx.conf | 14 ++-- www/.gitignore | 1 + www/core/Application.php | 14 +++- www/core/Response.php | 9 +++ www/core/Router.php | 42 ++++++++++-- www/pub/index.php | 12 ++-- www/views/404.php | 1 + www/views/contact.php | 20 ++++++ www/views/home.php | 1 + www/views/layouts/main.php | 44 +++++++++++++ 11 files changed, 244 insertions(+), 43 deletions(-) create mode 100644 www/.gitignore create mode 100644 www/core/Response.php create mode 100644 www/views/404.php create mode 100644 www/views/contact.php create mode 100644 www/views/home.php create mode 100644 www/views/layouts/main.php diff --git a/README.org b/README.org index 9f3efb3..c2bf2a9 100644 --- a/README.org +++ b/README.org @@ -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 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 + +#+end_src + +** Views +*** Home +#+begin_src php :tangle www/views/home.php +

Hello, World!

+#+end_src + +*** Contact +#+begin_src php :tangle www/views/contact.php +

Contact page

+ +
+
+
+ + +
We'll never share your email with anyone else.
+
+
+ + +
+
+ + +
+ +
+
+#+end_src + +*** 404 +#+begin_src php :tangle www/views/404.php +

404 - File not found!

+#+end_src diff --git a/dockerfiles/web/nginx.conf b/dockerfiles/web/nginx.conf index 8fe9f98..2775a51 100644 --- a/dockerfiles/web/nginx.conf +++ b/dockerfiles/web/nginx.conf @@ -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 index 0000000..57872d0 --- /dev/null +++ b/www/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/www/core/Application.php b/www/core/Application.php index 21f1063..e5567c0 100644 --- a/www/core/Application.php +++ b/www/core/Application.php @@ -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 index 0000000..06c2295 --- /dev/null +++ b/www/core/Response.php @@ -0,0 +1,9 @@ + diff --git a/www/core/Router.php b/www/core/Router.php index ccd884c..6589ac4 100644 --- a/www/core/Router.php +++ b/www/core/Router.php @@ -1,14 +1,18 @@ 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(); + } } ?> diff --git a/www/pub/index.php b/www/pub/index.php index 6efca35..5c05d91 100644 --- a/www/pub/index.php +++ b/www/pub/index.php @@ -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 index 0000000..90593e5 --- /dev/null +++ b/www/views/404.php @@ -0,0 +1 @@ +

404 - File not found!

diff --git a/www/views/contact.php b/www/views/contact.php new file mode 100644 index 0000000..d1c4d6c --- /dev/null +++ b/www/views/contact.php @@ -0,0 +1,20 @@ +

Contact page

+ +
+
+
+ + +
We'll never share your email with anyone else.
+
+
+ + +
+
+ + +
+ +
+
diff --git a/www/views/home.php b/www/views/home.php new file mode 100644 index 0000000..f76d8e9 --- /dev/null +++ b/www/views/home.php @@ -0,0 +1 @@ +

Hello, World!

diff --git a/www/views/layouts/main.php b/www/views/layouts/main.php new file mode 100644 index 0000000..b394359 --- /dev/null +++ b/www/views/layouts/main.php @@ -0,0 +1,44 @@ + + + + + + + + Document + + + + + +
+ {{content}} +
+ + + + + -- 2.52.0