src/Controller/ContactController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contact;
  4. use App\Form\ContactType;
  5. use App\Repository\ContactRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. class ContactController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/contact", name="contact")
  15.      */
  16.     public function index(Request $requestEntityManagerInterface $entityManager): Response
  17.     {
  18.         $contact = new Contact();
  19.         $form $this->createForm(ContactType::class, $contact);
  20.         $form->handleRequest($request);
  21.         if ($form->isSubmitted() && $form->isValid()) {
  22.             $entityManager->persist($contact);
  23.             $entityManager->flush();
  24.             $this->addFlash(
  25.                 'msg','Merci de nous avoir contacter'
  26.             );
  27.             return $this->redirectToRoute('contact', [], Response::HTTP_SEE_OTHER);
  28.         }
  29.         return $this->renderForm('contact/index.html.twig', [
  30.             'contact' => $contact,
  31.             'form' => $form,
  32.         ]);
  33.     }
  34.     /**
  35.      * @Route("admin/contact", name="contact_index", methods={"GET"})
  36.      */
  37.     public function contact(ContactRepository $contactRepository): Response
  38.     {
  39.         return $this->render('admin_contact/index.html.twig', [
  40.             'contacts' => $contactRepository->findAll(),
  41.         ]);
  42.     }
  43. }