src/Controller/HomeController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Feed;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  7. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class HomeController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("", name="index")
  17.      */
  18.     public function index(Request $requestEntityManagerInterface $em): Response
  19.     {
  20.         $fb = new Feed();
  21.         $form $this->createCrearForm($fb);
  22.         $form->handleRequest($request);
  23.         if ($form->isSubmitted() && $form->isValid()) {
  24.            
  25.             $em->persist($fb);
  26.             $em->flush();
  27.             $this->addFlash('success''¡Feed enviado correctamente! Gracias por su comentario.');
  28.             return $this->render('home/index.html.twig', [
  29.                 'form' => $form->createView(),
  30.             ]);
  31.         }
  32.         return $this->render('home/index.html.twig', [
  33.             'form' => $form->createView(),
  34.         ]);
  35.     }
  36.     /**
  37.     * Funcion auxiliar que crea el form para crear una pregunta
  38.     */
  39.     private function createCrearForm(Feed $entity)
  40.     {
  41.         $builder $this->createFormBuilder($entity);
  42.         $builder->setAction($this->generateUrl('index'));
  43.         $builder->setMethod('POST');
  44.         $builder->add('feedback'TextareaType::class, array('required' => true));
  45.         $builder->add('email'EmailType::class, array('required' => true));
  46.         $builder->add('nombre'TextType::class, array('required' => true));
  47.         $builder->add('_token'HiddenType::class,  array('mapped' => false));
  48.         return $builder->getForm();
  49.     }
  50. }