Add FrontController and UserController

pull/17/head
DahmaneYanis 2 years ago
parent 02dbf269f9
commit 26a27c291d

@ -6,6 +6,7 @@ require "../sql/database.php";
require "utils.php";
use App\Connexion;
use App\Controller\FrontController;
use App\Controller\EditorController;
use App\Controller\SampleFormController;
use App\Gateway\FormResultGateway;
@ -18,8 +19,6 @@ use App\Validation\ValidationFail;
use App\Controller\ErrorController;
$basePath = get_public_path();
$con = new Connexion(get_database());
@ -27,7 +26,9 @@ $con = new Connexion(get_database());
$router = new AltoRouter();
$router->setBasePath($basePath);
$frontController = new FrontController($router);
$frontController->route();
//$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig);
//$editorController = new EditorController(new TacticModel(new TacticInfoGateway($con)));
$frontController = new FrontController($router);

@ -1,27 +1,37 @@
<?php
namespace App\Controller;
use App\Controller\UserController;
use AltoRouter;
use App\Controller\ErrorController;
use App\Http\HttpCodes;
use App\Http\JsonHttpResponse;
use App\Http\ViewHttpResponse;
use App\Validation\ValidationFail;
use Twig\Loader\FilesystemLoader;
class FrontController{
private AltoRouter $router;
private UserControler $userControler;
private ?UserController $userController;
public function __construct(AltoRouter $router){
$this->router = $router;
$this->userController = new UserController();
}
public function main() {
$this->toRoute();
public function run() {
$this->route();
}
private function toRoute(){
$this->router->map("GET", "/", fn() => $this->userControler->home());
public function route()
{
$this->router->map("GET", "/", fn() => $this->userController->home());
// $router->map("GET", "/", fn() => $sampleFormController->displayFormReact());
// $router->map("POST", "/submit", fn() => $sampleFormController->submitFormReact($_POST));
// $router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig());
@ -33,13 +43,30 @@ class FrontController{
$match = $this->router->match();
if ($match == null) {
http_response_code(404);
$loader = new FilesystemLoader('../src/Views/');
$twig = new \Twig\Environment($loader);
http_response_code(HttpCodes::NOT_FOUND);
ErrorController::displayFailures([ValidationFail::notFound("Cette page n'existe pas")], $twig);
return;
}
$this->routeByResponseType($match);
}
private function routeByResponseType(array $match)
{
$response = call_user_func_array($match['target'], $match['params']);
http_response_code($response->getCode());
if ($response instanceof ViewHttpResponse) {
$this->displayByViewKind($response);
} else if ($response instanceof JsonHttpResponse) {
header('Content-type: application/json');
echo $response->getJson();
}
}
private function displayByViewKind(ViewHttpResponse $response){
$file = $response->getFile();
$args = $response->getArguments();
@ -52,20 +79,13 @@ class FrontController{
$loader = new FilesystemLoader('../src/Views/');
$twig = new \Twig\Environment($loader);
$twig->display($file, $args);
} catch (\Twig\Error\RuntimeError|\Twig\Error\SyntaxError $e) {
} catch (\Twig\Error\RuntimeError | \Twig\Error\SyntaxError $e) {
http_response_code(500);
echo "There was an error rendering your view, please refer to an administrator.\nlogs date: " . date("YYYD, d M Y H:i:s");
throw e;
}
break;
}
} else if ($response instanceof JsonHttpResponse) {
header('Content-type: application/json');
echo $response->getJson();
}
http_response_code($response->getCode());
}

@ -0,0 +1,17 @@
<?php
namespace App\Controller;
use App\Http\HttpResponse;
use App\Http\ViewHttpResponse;
class UserController {
public function home() : HttpResponse {
return ViewHttpResponse::twig("home.twig", []);
}
}
?>

@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Test</h1>
</body>
</html>
Loading…
Cancel
Save