diff --git a/lib/Framework/Controller.php b/lib/Framework/Controller.php index 62146f5..c36d74f 100644 --- a/lib/Framework/Controller.php +++ b/lib/Framework/Controller.php @@ -51,6 +51,10 @@ abstract class Controller */ public function render(string $template, array $vars = []): Response { + if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') { + $vars['flashes'] = $_SESSION['flashes']; + $_SESSION['flashes'] = []; + } $response = new Response($this->twig->render($template, $vars)); return $response; } @@ -59,4 +63,9 @@ abstract class Controller { return $this->kernel->getRouter()->getUrlForRoute($route); } + + public function addFlash(string $category, string $content): void + { + $_SESSION['flashes'][] = ['category' => $category, 'content' => $content]; + } } \ No newline at end of file diff --git a/lib/Framework/Twig.php b/lib/Framework/Twig.php index e016404..3352137 100644 --- a/lib/Framework/Twig.php +++ b/lib/Framework/Twig.php @@ -30,7 +30,7 @@ class Twig public function addFunctions() { - $path = new TwigFunction('path', function(string $route, array $params) { + $path = new TwigFunction('path', function(string $route, array $params = []) { return $this->kernel->getRouter()->getUrlForRoute($route, $params); }); $this->environment->addFunction($path); diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php index a687da7..cc00b3c 100644 --- a/src/Controller/SecurityController.php +++ b/src/Controller/SecurityController.php @@ -27,6 +27,7 @@ class SecurityController extends Controller $_SESSION['isAuthenticated'] = true; return new RedirectResponse($this->generateRoute('default')); } + $this->addFlash('danger', 'Username or password incorrect'); return new RedirectResponse($this->generateRoute('login')); } } \ No newline at end of file diff --git a/templates/base.html.twig b/templates/base.html.twig index eb069de..605a2e9 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -4,40 +4,36 @@