<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Routing\Annotation\Route;
use App\Form\ContactFormType;
class AccueilController extends AbstractController
{
/**
* @Route("/{_locale}", name="app_accueil", requirements={"_locale": "en|fr"}, defaults={"_locale": "fr"})
* @Route("/", name="app_accueil_default")
*/
public function index(Request $request, MailerInterface $mailer, string $_locale = 'fr'): Response
{
// Si on accède via la route par défaut sans locale, rediriger vers la locale par défaut
if ($request->attributes->get('_route') === 'app_accueil_default') {
$session = $request->getSession();
$locale = $session->get('_locale', 'fr');
return $this->redirectToRoute('app_accueil', ['_locale' => $locale], 301);
}
// Créer un formulaire vide
$form = $this->createForm(ContactFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
// Envoyer l'email
$email = (new Email())
->from($data['email'])
->to('commercial@matchnhire.com')
->subject('Nouveau message de contact depuis MatchnHire')
->html(
$this->renderView(
'emails/contact.html.twig',
['data' => $data]
)
);
$mailer->send($email);
// Ajouter un message flash et rediriger
$this->addFlash('success', $this->trans('form.success_message'));
return $this->redirectToRoute('app_accueil', ['_locale' => $_locale]);
}
return $this->render('accueil/index.html.twig', [
'form' => $form->createView(),
]);
}
}