src/Controller/ContactFormController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\MailOutBox;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  6. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  7. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. /**
  12.  * ContactForm controller.
  13.  *
  14.  * @Route("/contactForm")
  15.  */
  16. class ContactFormController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/", name="contactForm", methods={"GET", "POST"})
  20.      */
  21.     public function contactAction(
  22.         Request $request
  23.     )
  24.     {
  25.         $em $this->getDoctrine()->getManager();
  26.         $form $this->createFormBuilder()
  27.             ->setAction($this->generateUrl('contactForm'))
  28.             ->add('email'EmailType::class,[
  29.                 'label' => 'contact_form.email'
  30.             ])
  31.             ->add('name'TextType::class,[
  32.                 'label' => 'contact_form.name'
  33.             ])
  34.             ->add('body'TextareaType::class,[
  35.                 'label' => 'contact_form.message'
  36.             ])
  37.             ->add('add'SubmitType::class,[
  38.                 'label' => 'contact_form.send'
  39.             ])
  40.             ->getForm()
  41.         ;
  42.         if ($request->getMethod() == 'POST') {
  43.             $form->handleRequest($request);
  44.             if ($form->isValid()) {
  45.                 $mailOut = new MailOutBox();
  46.                 $mailOut->setRecipient('scorebox@fra-services.de');
  47.                 $mailOut->setSubject('Kontaktformular anfrage von ' $form->get('name')->getData());
  48.                 $mailOut->setBody('E-Mail: ' $form->get('email')->getData() . "<br>" .
  49.                 'Name: ' $form->get('name')->getData() . "<br>" .
  50.                 'Text: ' nl2br($form->get('body')->getData()));
  51. //                dump($mailOut);
  52. //                die;
  53.                 $em->persist($mailOut);
  54.                 $em->flush();
  55.                 return $this->redirect($request->headers->get('referer'));
  56.             }
  57.         }
  58.         return $this->render('includes/_contactform.html.twig',
  59.             array('form' => $form->createView())
  60.         );
  61.     }
  62. }