From: LeonardoBizzoni Date: Sun, 17 Apr 2022 10:55:11 +0000 (+0200) Subject: Generic controllers + multiple layouts X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=95e03bd399fece9a5093536b0aea5e57d116c304;p=highschool-graduation-project Generic controllers + multiple layouts --- diff --git a/README.org b/README.org index c2bf2a9..a70efaf 100644 --- a/README.org +++ b/README.org @@ -11,6 +11,7 @@ - [[#realizzazione][Realizzazione]] - [[#entry-point---indexphp][Entry point - index.php]] - [[#core][Core]] + - [[#controllers][Controllers]] - [[#views][Views]] * Descrizione del progetto @@ -141,16 +142,24 @@ La home page, si occupa di inizializzare l'applicazione ed impostare le route co #+begin_src php :tangle www/pub/index.php router->get("/", "home"); -$app->router->get("/contact", "contact"); +$app->router->get("/", [SiteController::class, "home"]); + +$app->router->get("/contact", [SiteController::class, "contact"]); +$app->router->post("/contact", [SiteController::class, "handleContact"]); + +# User authentication +$app->router->get("/login", [AuthController::class, "login"]); +$app->router->post("/login", [AuthController::class, "login"]); -$app->router->post("/contact", function() { - echo "handling submitted data"; -}); +$app->router->get("/register", [AuthController::class, "register"]); +$app->router->post("/register", [AuthController::class, "register"]); $app->run(); ?> @@ -165,6 +174,8 @@ La classe principale, si occupa di instanziare il Router e la Request helper cla namespace app\core; class Application { + private BaseController $controller; + public Router $router; public Request $req; public Response $res; @@ -184,6 +195,14 @@ class Application { public function run() { echo $this->router->resolve(); } + + public function getController() { + return $this->controller; + } + + public function setController(BaseController $controller) { + $this->controller = $controller; + } } ?> #+end_src @@ -252,30 +271,39 @@ class Router $callback = $this->routes[$method][$path] ?? false; if (is_string($callback)) { - echo $this->renderView($callback); - } else if ($callback) { - return call_user_func($callback); + return $this->renderView($callback); + } else if (is_array($callback)) { + Application::$app->setController(new $callback[0]); + $callback[0] = Application::$app->getController(); } else { $this->res->setStatusCode(404); - echo $this->renderView("404"); + return $this->renderView("404"); } + + return call_user_func($callback, $this->req); } - public function renderView(string $view) + public function renderView(string $view, array $params = []) { $layoutContent = $this->loadLayoutContent(); - $viewContent = $this->loadViewContent($view); + $viewContent = $this->loadViewContent($view, $params); return str_replace("{{content}}", $viewContent, $layoutContent); } private function loadLayoutContent() { + $layout = Application::$app->getController()->layout; ob_start(); - include_once Application::$ROOT_DIR."/views/layouts/main.php"; + include_once Application::$ROOT_DIR."/views/layouts/$layout.php"; return ob_get_clean(); } - private function loadViewContent(string $view) { + private function loadViewContent(string $view, array $params) { + # modo epico per creare variabili con lo stesso nome assegnato nell'array!! + foreach ($params as $key => $value) { + $$key = $value; + } + ob_start(); include_once Application::$ROOT_DIR."/views/$view.php"; return ob_get_clean(); @@ -311,10 +339,22 @@ class Request { } public function getBody() { - # TODO - } + $body = []; + + if ($this->getMethod() == "get") { + foreach ($_GET as $key => $value) { + $body[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS); + } + } + else if ($this->getMethod() == "post") { + foreach ($_POST as $key => $value) { + $body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS); + } + } + + return $body; + } } -?> #+end_src *** Response class @@ -330,29 +370,106 @@ class Response { ?> #+end_src +*** Base controller class +#+begin_src php :tangle www/core/BaseController.php +router->renderView($view, $params); + } + + public function setLayout(string $layout) { + $this->layout = $layout; + } +} +?> +#+end_src + +** Controllers +*** General controller +#+begin_src php :tangle www/controllers/SiteController.php + "Leonardo" + ]; + + return $this->render("home", $params); + } + + public function contact() { + return $this->render("contact"); + } + + public function handleContact(Request $req) { + $body = $req->getBody(); + + # $body validation + + return "Handling submitted data"; + } +} +?> +#+end_src + +*** Authentication controller +#+begin_src php :tangle www/controllers/AuthController.php +setLayout("auth"); + return $this->render("login"); + } + + public function register(Request $req) { + $this->setLayout("auth"); + if ($req->getMethod() == "post") { + return "Handling submitted data"; + } + return $this->render("register"); + } +} +#+end_src + ** Views *** Home #+begin_src php :tangle www/views/home.php

Hello, World!

+

Welcome, !

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

Contact page

-
+
- +
We'll never share your email with anyone else.
- +
- +
@@ -360,6 +477,67 @@ class Response {
#+end_src +*** Login +#+begin_src php :tangle www/views/login.php +

Login page

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

Registration page

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

404 - File not found!

diff --git a/www/controllers/AuthController.php b/www/controllers/AuthController.php new file mode 100644 index 0000000..e9b4da7 --- /dev/null +++ b/www/controllers/AuthController.php @@ -0,0 +1,20 @@ +setLayout("auth"); + return $this->render("login"); + } + + public function register(Request $req) { + $this->setLayout("auth"); + if ($req->getMethod() == "post") { + return "Handling submitted data"; + } + return $this->render("register"); + } +} diff --git a/www/controllers/SiteController.php b/www/controllers/SiteController.php new file mode 100644 index 0000000..913bf56 --- /dev/null +++ b/www/controllers/SiteController.php @@ -0,0 +1,28 @@ + "Leonardo" + ]; + + return $this->render("home", $params); + } + + public function contact() { + return $this->render("contact"); + } + + public function handleContact(Request $req) { + $body = $req->getBody(); + + # $body validation + + return "Handling submitted data"; + } +} +?> diff --git a/www/core/Application.php b/www/core/Application.php index e5567c0..9ffc416 100644 --- a/www/core/Application.php +++ b/www/core/Application.php @@ -2,6 +2,8 @@ namespace app\core; class Application { + private BaseController $controller; + public Router $router; public Request $req; public Response $res; @@ -21,5 +23,13 @@ class Application { public function run() { echo $this->router->resolve(); } + + public function getController() { + return $this->controller; + } + + public function setController(BaseController $controller) { + $this->controller = $controller; + } } ?> diff --git a/www/core/BaseController.php b/www/core/BaseController.php new file mode 100644 index 0000000..3e9f34d --- /dev/null +++ b/www/core/BaseController.php @@ -0,0 +1,15 @@ +router->renderView($view, $params); + } + + public function setLayout(string $layout) { + $this->layout = $layout; + } +} +?> diff --git a/www/core/Request.php b/www/core/Request.php index 05eb05c..e3ec72f 100644 --- a/www/core/Request.php +++ b/www/core/Request.php @@ -17,7 +17,19 @@ class Request { } public function getBody() { - # TODO - } + $body = []; + + if ($this->getMethod() == "get") { + foreach ($_GET as $key => $value) { + $body[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS); + } + } + else if ($this->getMethod() == "post") { + foreach ($_POST as $key => $value) { + $body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS); + } + } + + return $body; + } } -?> diff --git a/www/core/Router.php b/www/core/Router.php index 6589ac4..1bda1fc 100644 --- a/www/core/Router.php +++ b/www/core/Router.php @@ -33,30 +33,39 @@ class Router $callback = $this->routes[$method][$path] ?? false; if (is_string($callback)) { - echo $this->renderView($callback); - } else if ($callback) { - return call_user_func($callback); + return $this->renderView($callback); + } else if (is_array($callback)) { + Application::$app->setController(new $callback[0]); + $callback[0] = Application::$app->getController(); } else { $this->res->setStatusCode(404); - echo $this->renderView("404"); + return $this->renderView("404"); } + + return call_user_func($callback, $this->req); } - public function renderView(string $view) + public function renderView(string $view, array $params = []) { $layoutContent = $this->loadLayoutContent(); - $viewContent = $this->loadViewContent($view); + $viewContent = $this->loadViewContent($view, $params); return str_replace("{{content}}", $viewContent, $layoutContent); } private function loadLayoutContent() { + $layout = Application::$app->getController()->layout; ob_start(); - include_once Application::$ROOT_DIR."/views/layouts/main.php"; + include_once Application::$ROOT_DIR."/views/layouts/$layout.php"; return ob_get_clean(); } - private function loadViewContent(string $view) { + private function loadViewContent(string $view, array $params) { + # modo epico per creare variabili con lo stesso nome assegnato nell'array!! + foreach ($params as $key => $value) { + $$key = $value; + } + 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 5c05d91..e2ef83f 100644 --- a/www/pub/index.php +++ b/www/pub/index.php @@ -1,15 +1,23 @@ router->get("/", "home"); -$app->router->get("/contact", "contact"); +$app->router->get("/", [SiteController::class, "home"]); + +$app->router->get("/contact", [SiteController::class, "contact"]); +$app->router->post("/contact", [SiteController::class, "handleContact"]); + +# User authentication +$app->router->get("/login", [AuthController::class, "login"]); +$app->router->post("/login", [AuthController::class, "login"]); -$app->router->post("/contact", function() { - echo "handling submitted data"; -}); +$app->router->get("/register", [AuthController::class, "register"]); +$app->router->post("/register", [AuthController::class, "register"]); $app->run(); ?> diff --git a/www/views/contact.php b/www/views/contact.php index d1c4d6c..2df0027 100644 --- a/www/views/contact.php +++ b/www/views/contact.php @@ -1,18 +1,18 @@

Contact page

-
+
- +
We'll never share your email with anyone else.
- +
- +
diff --git a/www/views/home.php b/www/views/home.php index f76d8e9..0fb3949 100644 --- a/www/views/home.php +++ b/www/views/home.php @@ -1 +1,2 @@

Hello, World!

+

Welcome, !

diff --git a/www/views/layouts/auth.php b/www/views/layouts/auth.php new file mode 100644 index 0000000..9a0db66 --- /dev/null +++ b/www/views/layouts/auth.php @@ -0,0 +1,19 @@ + + + + + + + + Document + + + +
+ {{content}} +
+ + + + + diff --git a/www/views/layouts/main.php b/www/views/layouts/main.php index b394359..52b316e 100644 --- a/www/views/layouts/main.php +++ b/www/views/layouts/main.php @@ -11,23 +11,23 @@