src/Controller/ContactController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ContactType;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use App\Service\Api;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\Mime\Email;
  15. class ContactController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/contact", name="contact")
  19.      * @param Request         $request
  20.      * @param MailerInterface $mailer
  21.      * @return Response
  22.      */
  23.     public function index(Request $requestMailerInterface $mailerTranslatorInterface $translatorEntityManagerInterface $managerApi $apiService): Response
  24.     {
  25.         $form $this->createForm(ContactType::class);
  26.         $form->handleRequest($request);
  27.         if ($form->isSubmitted()) {
  28.             if ($form->isValid()) {
  29.                 $email = (new Email())
  30.                     ->from(new Address($this->getParameter('app.contactEmail'), 'CONTACT FORM'))
  31.                     ->to(new Address($this->getParameter('app.supportEmail')))
  32.                     ->subject($form->getData()['subject'])
  33.                     ->html(
  34.                         $this->renderView(
  35.                             'emails/contact-us.html.twig',
  36.                             ['contact' => $form->getData()]
  37.                         )
  38.                     );
  39.                     $mailer->send($email);
  40.                 $this->addFlash('success'$translator->trans("Your message has been sent successfully, one of our experts will contact you as soon as possible. Thank you for your trust."));
  41.                 return $this->redirect($request->getUri());
  42.             } else {
  43.                 $this->addFlash("danger"$translator->trans("An error has occurred while sending your request! please check the form for errors and try again."));
  44.             }
  45.         }
  46.         $data['contactForm'] = $form->createView();
  47.         $data['services'] = $apiService->getAllServices();
  48.         $data['icons'] = $apiService->getServicesIcons();
  49.         return $this->render('contact/index.html.twig'$data);
  50.     }
  51. }