src/Controller/LoginController.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Cookie;
  9. class LoginController extends AbstractController
  10. {
  11.     #[Route('/login', name: 'app_login')]
  12.     public function index(AuthenticationUtils $authenticationUtils, Request $request): Response
  13.     {
  14.         $request = $this->get('request_stack')->getCurrentRequest();
  15.         $eventId = $request->cookies->get('current_event_id');
  16.         $error = $authenticationUtils->getLastAuthenticationError();
  17.         $lastUsername = $authenticationUtils->getLastUsername();
  18.         if(!is_null($eventId)) {
  19.             return $this->render('login/index.html.twig', [
  20.                 'last_username' => $lastUsername,
  21.                 'error'         => $error,
  22.             ]);
  23.         }
  24.         // Le cookie n'existe pas, donc nous allons le créer avec une valeur par défaut de 1
  25.         $cookie = new Cookie('current_event_id', 1, time() + (2 * 365 * 24 * 60 * 60));  // expire dans 2 ans
  26.         // Créez une nouvelle réponse pour pouvoir ajouter le cookie
  27.         $response = $this->render('login/index.html.twig', [
  28.             'last_username' => $lastUsername,
  29.             'error'         => $error,
  30.         ]);
  31.         $response->headers->setCookie($cookie);
  32.         // Retournez la réponse qui contient maintenant le cookie
  33.         return $response;
  34.     }
  35. }