src/Controller/HomeController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Alergeno;
  4. use App\Entity\Feed;
  5. use Doctrine\ORM\EntityManager;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  10. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class HomeController extends AbstractController
  17. {
  18.     public function __construct(LoggerInterface $logger)
  19.     {
  20.         $this->logger $logger;
  21.     }
  22.     /**
  23.      * @Route("", name="index")
  24.      */
  25.     public function index(Request $requestEntityManagerInterface $em): Response
  26.     {
  27.         $fb = new Feed();
  28.         $form $this->createCrearForm($fb);
  29.         $form->handleRequest($request);
  30.         if ($form->isSubmitted() && $form->isValid()) {
  31.             $em->persist($fb);
  32.             $em->flush();
  33.             $this->addFlash('success''¡Feed enviado correctamente! Gracias por su comentario.');
  34.             return $this->render('home/index.html.twig', [
  35.                 'form' => $form->createView(),
  36.             ]);
  37.         }
  38.         return $this->render('home/index.html.twig', [
  39.             'form' => $form->createView(),
  40.         ]);
  41.     }
  42.     /**
  43.      * @Route("prueba", name="prueba")
  44.      */
  45.     public function prueba(Request $requestEntityManagerInterface $em)
  46.     {
  47.         $serializer $this->get('serializer');
  48.         $this->logger->info('Testing prueba');
  49.         $repository $em->getRepository('Gedmo\\Translatable\\Entity\\Translation');
  50.         // it works for ODM also
  51.         $article $em->find(Alergeno::class, /*article id*/);
  52.         return new Response($serializer->serialize(
  53.             $article,
  54.             "json",
  55.             ['groups' => ['alergeno']]
  56.         ), Response::HTTP_OK);
  57.     }
  58.     /**
  59.      * @Route("lang", name="lang")
  60.      */
  61.     public function lang(Request $requestEntityManagerInterface $em)
  62.     {
  63.         $request->setLocale($request->get('_locale'));
  64.         $request->getSession()->set('_locale'$request->get('_locale'));
  65.         $referer $request->headers->get('referer');
  66.         return $this->redirect($referer);
  67.     }
  68.     /**
  69.      * Funcion auxiliar que crea el form para crear una pregunta
  70.      */
  71.     private function createCrearForm(Feed $entity)
  72.     {
  73.         $builder $this->createFormBuilder($entity);
  74.         $builder->setAction($this->generateUrl('index'));
  75.         $builder->setMethod('POST');
  76.         $builder->add('feedback'TextareaType::class, array('required' => true));
  77.         $builder->add('email'EmailType::class, array('required' => true));
  78.         $builder->add('nombre'TextType::class, array('required' => true));
  79.         $builder->add('_token'HiddenType::class,  array('mapped' => false));
  80.         return $builder->getForm();
  81.     }
  82. }