src/Controller/ContactController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Events;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Boab\CmsBundle\Controller\BaseController;
  6. use Symfony\Component\Routing\RouterInterface;
  7. use Boab\CmsBundle\Controller\PublicControllerInterface;
  8. use App\Form\ContactType;
  9. use App\Model\Mail;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Mime\Address;
  16. class ContactController extends BaseController implements PublicControllerInterface
  17. {
  18.     protected $router;
  19.     protected $entityManager;
  20.     public function __construct(
  21.         RouterInterface $router
  22.         EntityManagerInterface $entityManager,
  23.         private LoggerInterface $logger)
  24.     {
  25.         $this->router $router;
  26.         $this->entityManager $entityManager;
  27.     }
  28.     public function contactAction(Request $requestMailerInterface $mailer)
  29.     {
  30.         $action $this->router->generate('app.contact_us');
  31.         $form $this->createForm(ContactType::class, new Mail, [
  32.             'action' => $action,
  33.             'method' => 'POST',
  34.         ]);
  35.         $view $this->viewManager->load('page/contact_form.html.twig');
  36.         $view['form'] = $form->createView();     
  37.         return new Response($view->render());
  38.     }
  39.     public function checkContactAction(Request $requestMailerInterface $mailer)
  40.     {
  41.         $form $this->createForm(ContactType::class, new Mail);
  42.         $form->handleRequest($request);
  43.         if($form->isSubmitted() && !$form->isValid()) {
  44.             $this->flash->setErrors($this->getFormErrors($form));
  45.             return $this->redirect($this->router->generate('contact_us'), 301);
  46.         }
  47.         try{
  48.             $mail $form->getData();
  49.             $this->eventDispatcher->dispatch($mailEvents::VISITOR_SEND_CONTACT);
  50.         }catch(\Exception $e){
  51.             $this->logger->error($e->getMessage(), ['exception'=>$e]);
  52.         }
  53.         $this->flash->setSuccess('Message sent successfully');
  54.         return $this->redirect($this->router->generate('contact_us', ['status'=>'success']));        
  55.     }  
  56. }