From 382df5b7f617395abd5030438a4766491ea5e606 Mon Sep 17 00:00:00 2001 From: LeonardoBizzoni Date: Thu, 14 Apr 2022 08:33:10 +0200 Subject: [PATCH] Added class doc --- README.org | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/README.org b/README.org index a232bd5..c7ca65b 100644 --- a/README.org +++ b/README.org @@ -122,6 +122,8 @@ La web app è strutturata seguendo il *Model View Controller* framework. Per gestire le dipendenze e namespaces di PHP viene utilizzato *composer*. ** Entry point - index.php +La home page, si occupa di inizializzare l'applicazione ed impostare le route con annessa funzione di callback o *View*. + #+begin_src php :tangle www/pub/index.php run(); ** Core *** Main application class +La classe principale, si occupa di instanziare il Router e la Request helper class. + #+begin_src php :tangle www/core/Application.php "method". + +#+begin_example +{ + ["get"] => { + ["/"] => func(), + ["/test"] => func(), + }, + + ["post"] => { + ["/"] => func(), + ["/test"] => func(), + } +} +#+end_example + +Metodi: +- "get()": imposta la route con metodo get +- "post()": imposta la route con metodo post +- "resolve()": utilizzando la Request helper class ricava quale funzione di callback chiamare sullo specifico path e metodo richiesto + #+begin_src php :tangle www/core/Router.php routes["get"][$path] = $callback; } + public function post($path, $callback) + { + $this->routes["post"][$path] = $callback; + } + public function resolve() { @@ -189,10 +225,10 @@ class Router $method = $this->req->getMethod(); $callback = $this->routes[$method][$path] ?? false; - if (!$callback) { - echo "Not found!"; - } else { + if ($callback) { echo call_user_func($callback); + } else { + echo "Not found!"; } } } @@ -200,6 +236,12 @@ class Router #+end_src *** Request class +Una classe helper, analizza le requests e restituisce l'informazione necessaria al richiedente. + +Metodi: +- "getPath()": restituisce il path richiesto, se viene passata una query string insieme al path essa non viene restituita. (Utile a Router resolve()) +- "getMethod()": restituisce il metodo utilizzato nella request in minuscolo. (Utile a Router resolve()) + #+begin_src php :tangle www/core/Request.php