src/Controller/HomeController.php line 20

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