src/Controller/ContactController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\{ResponseRequest};
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Mailer\MailerInterface;
  7. use App\Entity\Contact;
  8. use App\Form\ContactType;
  9. use App\Services\{ContactService as Service};
  10. class ContactController extends AbstractController
  11. {
  12.     #[Route('/contact'name'app_contact')]
  13.     public function index(Request $requestService $service): Response
  14.     {
  15.         $contact = new Contact();
  16.         $contactForm $this->createForm(ContactType::class, $contact);
  17.         $contactForm->handleRequest($request);
  18.         if ($contactForm->isSubmitted() && $contactForm->isValid()) {
  19.             try {
  20.                 $contact->setCreatedAt(new \DateTime());
  21.                 $contact->setType(Contact::TYPE_FORMCONTACT);
  22.                 $em $this->getDoctrine()->getManager();
  23.                 $em->persist($contact);
  24.                 $em->flush();
  25.                 $service->notifyResponsible($contact$this->getParameter('contact_form.subject'), $this->getParameter('app_mailer_name'));
  26.                 $this->addFlash(
  27.                     'success',
  28.                     'Message envoyé !'
  29.                 );
  30.             } catch (\Throwable $th) {
  31.                 $this->addFlash(
  32.                     'warning',
  33.                     'Erreur d\'envoie !'
  34.                 );
  35.             }
  36.         }
  37.         return $this->render('contact/index.html.twig', [
  38.             'contactForm' => $contactForm->createView(),
  39.         ]);
  40.     }
  41. }