src/Controller/MainController.php line 18
<?php
namespace Cms\Controller;
use Cms\Contact\ContactDto;
use Cms\Contact\ContactMailer;
use Cms\Form\ContactType;
use Cms\Repository\PostRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class MainController extends AbstractController
{
#[Route('', name: 'cms_main_index', methods: ['GET'])]
public function index(PostRepository $postRepository): Response
{
$lastPost = $postRepository->findOneBy(['published' => true, 'deleted' => false], ['publishedAt' => 'DESC']);
return $this->render('main/index.html.twig', [
'lastPost' => $lastPost,
])
->setSharedMaxAge(3600);
}
#[Route('/contact', name: 'cms_main_contact', methods: ['GET', 'POST'])]
public function contact(Request $request, ContactMailer $mailer, TranslatorInterface $translator): Response
{
$dto = new ContactDto();
$form = $this->createForm(ContactType::class, $dto);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$dto->setCreatedAt(new \DateTimeImmutable());
$sent = $mailer->sendMail($dto);
[$level, $message] = match ($sent) {
true => ['success', $translator->trans('Merci, votre mesage a bien été envoyé.')],
false => ['error', $translator->trans("Une erreur s'est produite, veuillez réessayer.")],
};
$this->addFlash($level, $message);
return $this->render('main/contact/confirmation_sent.frame.html.twig');
}
return $this->render('main/contact.html.twig', [
'form' => $form,
]);
}
}