src/EventSubscriber/AuthenticationFormsSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Form\Website\Type\Authentication\ConnectionType;
  4. use App\Form\Website\Type\Authentication\ForgotPasswordType;
  5. use App\Form\Website\Type\Authentication\SubscriptionType;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Form\FormFactoryInterface;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Twig\Environment;
  11. /**
  12. * Subscriber pour rendre les formulaires d'authentification disponibles globalement dans Twig
  13. */
  14. class AuthenticationFormsSubscriber implements EventSubscriberInterface
  15. {
  16. private FormFactoryInterface $formFactory;
  17. private Environment $twig;
  18. public function __construct(FormFactoryInterface $formFactory, Environment $twig)
  19. {
  20. $this->formFactory = $formFactory;
  21. $this->twig = $twig;
  22. }
  23. public static function getSubscribedEvents(): array
  24. {
  25. return [
  26. KernelEvents::CONTROLLER => 'onKernelController',
  27. ];
  28. }
  29. public function onKernelController(ControllerEvent $event): void
  30. {
  31. // Ne s'exécuter que dans le contexte website (pas admin)
  32. $request = $event->getRequest();
  33. if (!$request || strpos($request->getPathInfo(), '/admin') === 0) {
  34. return;
  35. }
  36. // Créer les formulaires d'authentification
  37. $connectionForm = $this->formFactory->create(ConnectionType::class);
  38. $forgotPasswordForm = $this->formFactory->create(ForgotPasswordType::class);
  39. $subscriptionForm = $this->formFactory->create(SubscriptionType::class);
  40. // Les rendre disponibles globalement dans Twig
  41. $this->twig->addGlobal('connectionForm', $connectionForm->createView());
  42. $this->twig->addGlobal('forgotPasswordForm', $forgotPasswordForm->createView());
  43. $this->twig->addGlobal('subscriptionForm', $subscriptionForm->createView());
  44. }
  45. }