src/Controller/ContactController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contact;
  4. use App\Form\ContactType;
  5. use App\Notification\ContactNotification;
  6. use App\Repository\SectionTitleRepository;
  7. use App\Repository\TitlesRepository;
  8. use App\Service\SendMailService;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Notifier\NotifierInterface;
  14. use Symfony\Component\Notifier\Notification\Notification;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class ContactController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/{_locale<%app.supported_locales%>}/contact", name="app_contact")
  20.      */
  21.     public function index(TitlesRepository $sectionTitleRepository, Request $request, ContactNotification $notification, SectiontitleRepository $postRepo, NotifierInterface $notifier): Response
  22.     {
  23.         $title = $sectionTitleRepository->findTitleByTag('contact_index');
  24.         $contact = new Contact;
  25.         $form = $this->createForm(ContactType::class, $contact);
  26.         $form->handleRequest($request);
  27.         if ($form->isSubmitted() && $form->isValid()) {
  28.             $notification->notify($contact);
  29.             $notifier->send(new Notification('Votre message a bien été envoyé. Nous vous contacterons sous peu.', ['browser']));
  30.             return $this->redirectToRoute('app_contact');
  31.         }
  32.         return $this->render('contact/index.html.twig', [
  33.             'contactTitle' => $title,
  34.             'form' => $form->createView()
  35.         ]);
  36.     }
  37. }