- [[#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:
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.
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();
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();
}
}
?>
#+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)
$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
}
?>
#+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
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;
+ }
}
}
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();
}
}
?>
--- /dev/null
+<?php
+namespace app\core;
+
+class Response {
+ public function setStatusCode(int $code) {
+ http_response_code($code);
+ }
+}
+?>
<?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)
$this->routes["get"][$path] = $callback;
}
+ public function post($path, $callback)
+ {
+ $this->routes["post"][$path] = $callback;
+ }
+
public function resolve()
{
$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();
+ }
}
?>
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();
--- /dev/null
+<h1>404 - File not found!</h1>
--- /dev/null
+<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>
--- /dev/null
+<h1>Hello, World!</h1>
--- /dev/null
+<!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>