From ae375b87385ef2fbc4a411d98cc949ba1025c1ca Mon Sep 17 00:00:00 2001 From: "vivien.dufour" Date: Fri, 10 Nov 2023 14:18:21 +0100 Subject: [PATCH] changed php error view to twig error view + added css --- public/index.php | 5 +++-- src/Controller/ErrorController.php | 34 ++++++++++++++++++++++++++++++ src/View/error.php | 23 -------------------- src/Views/error.html.twig | 29 +++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 src/Controller/ErrorController.php delete mode 100644 src/View/error.php create mode 100644 src/Views/error.html.twig diff --git a/public/index.php b/public/index.php index 4c5290b..455da20 100644 --- a/public/index.php +++ b/public/index.php @@ -35,6 +35,7 @@ $router = new AltoRouter(); $router->setBasePath($basePath); $sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); +$errorController = new \App\Controller\ErrorController(new FormResultGateway($con), $twig); $router->map("GET", "/", fn() => $sampleFormController->displayForm()); $router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST)); $router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); @@ -43,9 +44,9 @@ $router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTw $match = $router->match(); if ($match == null) { - // TODO redirect to a 404 not found page instead (issue #1) + $error = "404"; + $errorController->displayError($error); http_response_code(404); - echo "Page non trouvée"; exit(1); } diff --git a/src/Controller/ErrorController.php b/src/Controller/ErrorController.php new file mode 100644 index 0000000..07d9381 --- /dev/null +++ b/src/Controller/ErrorController.php @@ -0,0 +1,34 @@ +gateway = $gateway; + $this->twig = $twig; + } + + public function displayError($error) { + $error = array("error" => $error); + try { + echo $this->twig->render('error.html.twig', $error); + } catch (LoaderError | RuntimeError | SyntaxError $e) { + echo "Twig error: $e"; + } + } +} diff --git a/src/View/error.php b/src/View/error.php deleted file mode 100644 index 3bd7828..0000000 --- a/src/View/error.php +++ /dev/null @@ -1,23 +0,0 @@ - - - Error - - - - -

IQBall

- -Erreur 404"; - echo "

Cette page n'existe pas

"; - } -} -?> - - - - - \ No newline at end of file diff --git a/src/Views/error.html.twig b/src/Views/error.html.twig new file mode 100644 index 0000000..30290ae --- /dev/null +++ b/src/Views/error.html.twig @@ -0,0 +1,29 @@ + + + + + Error + + {% block stylesheets %} + + {% endblock %} + + + + +

IQBall

+ +{% if error == "404" %} +

Erreur 404 : cette page n'existe pas

+{% endif %} + + + + + + \ No newline at end of file