src/Controller/Front/PageController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use App\Service\AppServices;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use Symfony\Component\Validator\Constraints\NotBlank;
  12. use Symfony\Component\Validator\Constraints\Length;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. class PageController extends AbstractController {
  15.     /**
  16.      * @Route("/page/{slug}", name="page")
  17.      */
  18.     public function page($slugAppServices $servicesTranslatorInterface $translator) {
  19.         $page $services->getPages(array('slug' => $slug))->getQuery()->getOneOrNullResult();
  20.         if (!$page) {
  21.             $this->addFlash("error"$translator->trans("The page can not be found"));
  22.             return $this->redirectToRoute("homepage");
  23.         }
  24.         return $this->render('Front/Page/page.html.twig', [
  25.                     "page" => $page
  26.         ]);
  27.     }
  28.     /**
  29.      * @Route("/contact", name="contact")
  30.      */
  31.     public function contact(Request $requestTranslatorInterface $translator, \Swift_Mailer $mailerAppServices $services) {
  32.         $form $this->createFormBuilder()
  33.                 ->add('name'TextType::class, [
  34.                     'required' => true,
  35.                     'label' => 'Name',
  36.                     'constraints' => array(
  37.                         new NotBlank()
  38.                     ),
  39.                 ])
  40.                 ->add('email'TextType::class, [
  41.                     'required' => true,
  42.                     'label' => 'Email',
  43.                     'constraints' => array(
  44.                         new NotBlank(),
  45.                         new Assert\Email([
  46.                             'checkMX' => true,
  47.                                 ])
  48.                     ),
  49.                 ])
  50.                 ->add('subject'TextType::class, [
  51.                     'required' => true,
  52.                     'label' => 'Subject',
  53.                     'constraints' => array(
  54.                         new NotBlank(),
  55.                         new Length([
  56.                             'min' => 2,
  57.                             'max' => 20,
  58.                             'allowEmptyString' => false,
  59.                                 ])
  60.                     ),
  61.                 ])
  62.                 ->add('message'TextareaType::class, [
  63.                     'required' => true,
  64.                     'label' => 'Message',
  65.                     'attr' => ['rows' => '10'],
  66.                     'constraints' => array(
  67.                         new NotBlank(),
  68.                         new Length([
  69.                             'min' => 10,
  70.                             'max' => 500,
  71.                             'allowEmptyString' => false,
  72.                                 ])
  73.                     ),
  74.                 ])
  75.                 ->add('save'SubmitType::class, [
  76.                     'label' => 'Send',
  77.                     'attr' => ['class' => 'btn btn-primary btn-block'],
  78.                 ])
  79.                 ->getForm();
  80.         $form->handleRequest($request);
  81.         if ($form->isSubmitted()) {
  82.             if ($form->isValid()) {
  83.                 $data $form->getData();
  84.                 $email = (new \Swift_Message($services->getSetting("website_name") . " contact form"))
  85.                         ->setFrom($services->getSetting('no_reply_email'))
  86.                         ->setTo($services->getSetting("contact_email"))
  87.                         ->setBody("Sender: " $data["name"] . "\r\n" .
  88.                         "Email: " $data["email"] . "\r\n" .
  89.                         "Subject: " $data["subject"] . "\r\n" .
  90.                         "Message: " $data["message"] . "\r\n");
  91.                 $mailer->send($email);
  92.                 $this->addFlash('success'$translator->trans('Your message has been successfully sent'));
  93.             } else {
  94.                 $this->addFlash('error'$translator->trans('The form contains invalid data'));
  95.             }
  96.         }
  97.         return $this->render('Front/Page/contact.html.twig', ['form' => $form->createView()]);
  98.     }
  99.     /**
  100.      * @Route("/access-denied", name="access_denied")
  101.      */
  102.     public function accessDenied() {
  103.         return $this->render('Front/Page/access-denied.html.twig');
  104.     }
  105. }