src/Controller/Front/FormController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Controller\Core\BaseFrontController;
  4. use App\Entity\FormContact;
  5. use App\Form\Front\FormContactType;
  6. use App\Manager\ContactManager;
  7. use App\Manager\FormContactManager;
  8. use App\Service\Mail\ContactMail;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class FormController extends BaseFrontController
  14. {
  15.     const FORM_CONTACT 'sd_form_contact_entity';
  16.     /**
  17.      * @Route("/contacto", name="contact", methods={"GET"})
  18.      * @Template("front/form/contact.html.twig")
  19.      */
  20.     public function contact(ContactManager $contactManager): array
  21.     {
  22.         $entity $this->getFormSession(self::FORM_CONTACT, new FormContact());
  23.         $form $this->getFormView(FormContactType::class, $entity'contact_action');
  24.         $this->locals['sd'] = $contactManager->find(1);
  25.         $this->locals['form'] = $form;
  26.         return $this->locals;
  27.     }
  28.     /**
  29.      * @Route("/contacto", name="contact_action", methods={"POST"})
  30.      */
  31.     public function contactAction(Request $requestFormContactManager $managerContactMail $mail): RedirectResponse
  32.     {
  33.         $entity = new FormContact();
  34.         $form $this->getForm(FormContactType::class, $entity'contact_action');
  35.         $form->handleRequest($request);
  36.         if ($form->isValid() && $this->captchaValidate()) {
  37.             $manager->save($entity);
  38.             $mail->load($entity)->send();
  39.             return $this->redirectToRoute('thanks_slug', ['slug' => 'contacto']);
  40.         }
  41.         $this->saveFormSession(self::FORM_CONTACT$entity);
  42.         return $this->redirectToRoute('contact');
  43.     }
  44. }