|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Controller; |
| 6 | + |
| 7 | +use App\Form\GuestbookType; |
| 8 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 9 | +use Symfony\Component\HttpFoundation\Request; |
| 10 | +use Symfony\Component\HttpFoundation\Response; |
| 11 | +use Symfony\Component\Routing\Attribute\Route; |
| 12 | + |
| 13 | +class GuestbookController extends AbstractController |
| 14 | +{ |
| 15 | + #[Route('/guestbook', methods: ['POST', 'GET'])] |
| 16 | + public function index(Request $request): Response |
| 17 | + { |
| 18 | + $symfonyWayForm = $this->container->get('form.factory')->createNamed('sf_guestbook', GuestbookType::class); |
| 19 | + $symfonyWayForm->handleRequest($request); |
| 20 | + if ($symfonyWayForm->isSubmitted() && $symfonyWayForm->isValid()) { |
| 21 | + // Save |
| 22 | + return $this->redirectToRoute('app_guestbook_success'); |
| 23 | + } |
| 24 | + |
| 25 | + $htmxWayForm = $this->container->get('form.factory')->createNamed('htmx_guestbook', GuestbookType::class); |
| 26 | + $htmxWayForm->handleRequest($request); |
| 27 | + if ($htmxWayForm->isSubmitted() && $htmxWayForm->isValid()) { |
| 28 | + // Save |
| 29 | + $response = $this->render('guestbook/success.html.twig'); |
| 30 | + |
| 31 | + // Not mandatory, only to be compliant with the "old way". |
| 32 | + $response->headers->set('HX-Push-Url', $this->generateUrl('app_guestbook_success')); |
| 33 | + |
| 34 | + return $response; |
| 35 | + } |
| 36 | + |
| 37 | + return $this->render('guestbook/index.html.twig', [ |
| 38 | + 'symfonyWayForm' => $symfonyWayForm, |
| 39 | + 'htmxWayForm' => $htmxWayForm, |
| 40 | + ]); |
| 41 | + } |
| 42 | + |
| 43 | + #[Route('/guestbook/success', methods: ['POST', 'GET'])] |
| 44 | + public function success(Request $request): Response |
| 45 | + { |
| 46 | + return $this->render('guestbook/success.html.twig'); |
| 47 | + } |
| 48 | +} |
0 commit comments