<?php
namespace App\Controller;
use App\Entity\Contact;
use App\Form\Type\ContactType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ContactController extends AbstractController
{
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @Route("/contact", name="contact")
*/
public function contact(Request $request): Response
{
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contact = $form->getData();
// Persist contact message
$this->entityManager->persist($contact);
$this->entityManager->flush();
$this->addFlash('success', 'Message envoyé !');
return $this->redirectToRoute('contact');
}
return $this->render('contact/contact.html.twig', [
'controller_name' => 'ContactController',
'active_menu' => 'contact',
'form' => $form->createView()
]);
}
}