src/Controller/ContactController.php line 32

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 Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use DateTimeImmutable;
  11. /**
  12.  * @Route("/contact")
  13.  */
  14. class ContactController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/index", name="app_contact_index", methods={"GET"})
  18.      */
  19.     public function index(ContactRepository $contactRepository): Response
  20.     {
  21.         return $this->render('contact/index.html.twig', [
  22.             'contacts' => $contactRepository->findAll(),
  23.         ]);
  24.     }
  25.     /**
  26.      * @Route("/", name="app_contact_new", methods={"GET", "POST"})
  27.      */
  28.     public function new(Request $requestContactRepository $contactRepository): Response
  29.     {
  30.         $contact = new Contact();
  31.         $form $this->createForm(ContactType::class, $contact);
  32.         $form->handleRequest($request);
  33.         if ($form->isSubmitted() && $form->isValid()) {
  34.             $contact->setCreatedAt(new DateTimeImmutable());
  35.             $contact->setResolved(false);
  36.             $contactRepository->add($contacttrue);
  37.             $this->addFlash('success''info contact bien prise en compte!');
  38.             return $this->redirectToRoute('app_home', [], Response::HTTP_SEE_OTHER);
  39.         }
  40.         return $this->renderForm('contact/new.html.twig', [
  41.             'form' => $form,
  42.         ]);
  43.     }
  44.     /**
  45.      * @Route("/{id}", name="app_contact_show", methods={"GET"})
  46.      */
  47.     public function show(Contact $contact): Response
  48.     {
  49.         return $this->render('contact/show.html.twig', [
  50.             'contact' => $contact,
  51.         ]);
  52.     }
  53.     /**
  54.      * @Route("/{id}/edit", name="app_contact_edit", methods={"GET", "POST"})
  55.      */
  56.     public function edit(Request $requestContact $contactContactRepository $contactRepository): Response
  57.     {
  58.         $form $this->createForm(ContactType::class, $contact);
  59.         $form->handleRequest($request);
  60.         if ($form->isSubmitted() && $form->isValid()) {
  61.             $contactRepository->add($contacttrue);
  62.             $this->addFlash('success''modification info contact bien prise en compte!');
  63.             return $this->redirectToRoute('app_contact_index', [], Response::HTTP_SEE_OTHER);
  64.         }
  65.         return $this->renderForm('contact/edit.html.twig', [
  66.             'contact' => $contact,
  67.             'form' => $form,
  68.         ]);
  69.     }
  70.     /**
  71.      * @Route("/{id}", name="app_contact_delete", methods={"POST"})
  72.      */
  73.     public function delete(Request $requestContact $contactContactRepository $contactRepository): Response
  74.     {
  75.         if ($this->isCsrfTokenValid('delete' $contact->getId(), $request->request->get('_token'))) {
  76.             $contactRepository->remove($contacttrue);
  77.         }
  78.         $this->addFlash('warning''info contact suppression bien prise en compte!');
  79.         return $this->redirectToRoute('app_contact_index', [], Response::HTTP_SEE_OTHER);
  80.     }
  81. }