src/Controller/ContactController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use App\Entity\Email;
  8. use App\Form\EmailType;
  9. class ContactController extends AbstractController
  10. {
  11.     /**
  12.      * @Route("/contact", name="app_contact")
  13.      */
  14.     public function index(Request $request\Swift_Mailer $mailer ): Response
  15.     {
  16.         $enquiry = new Email();
  17.         $form $this->createForm(EmailType::class, $enquiry);
  18.         $form->handleRequest($request);
  19.         //dd($form->getErrors());
  20.         if ($form->isSubmitted() && $form->isValid()) {
  21.             $enquiry->setSender($this->getUser());
  22.             $message = (new \Swift_Message($enquiry->getSubject()))
  23.                 ->setFrom($this->getUser()->getEmail())
  24.                 ->setTo('[email protected]')
  25.                 ->setBody($enquiry->getContent());
  26.             $mailer->send($message);
  27.             $entityManager $this->getDoctrine()->getManager();
  28.             $entityManager->persist($enquiry);
  29.             $entityManager->flush();
  30.             $this->addFlash('primary''Correspondance bien transmis. Nous vous repondrons dans les plus bref delais!');
  31.         }
  32.         return $this->render('contact/form.html.twig', array(
  33.             'form' => $form->createView()
  34.         ));
  35.     }
  36. }