src/Controller/ContactController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contact;
  4. use App\Form\ContactType;
  5. use Doctrine\ORM\EntityManagerInterface;
  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. class ContactController extends AbstractController
  11. {
  12.     public function __construct(EntityManagerInterface $manager)
  13.     {
  14.         $this->manager $manager;
  15.     }
  16.     /* ------------------ Poster un contact ----------------*/
  17.     /**
  18.      * @Route("/contact", name="app_contact")
  19.      */
  20.     public function index(Request $request): Response
  21.     {
  22.         // Création d'une instance "contact"
  23.         $contact = new Contact();
  24.         // Création du formulaire de contact à partir de ContactType.php
  25.         $formContact $this->createForm(ContactType::class, $contact);
  26.         // Traitement du formulaire formContact
  27.         $formContact->handleRequest($request);
  28.         // Si le formulaire est soumis et validé , alors...
  29.         if ($formContact->isSubmitted() && $formContact->isValid()) {
  30.             // On persiste le contact
  31.             $this->manager->persist($contact);
  32.             // On flush 
  33.             $this->manager->flush();
  34.         }
  35.         return $this->render('contact/index.html.twig', [
  36.             'controller_name' => 'ContactController',
  37.             'myContact' => $formContact->createView(),
  38.         ]);
  39.     }
  40.     /* ------------------ Effacer le contact ---------------- */
  41.     /**
  42.      * @Route("/contact/delete/{id}", name="app_contact_delete")
  43.      */
  44.     public function articleDelete(Contact $contactRequest $request): Response
  45.     {
  46.         // On efface le contact
  47.         $this->manager->remove($contact);
  48.         // On flush
  49.         $this->manager->flush();
  50.         return $this->redirectToRoute('app_home');
  51.     }
  52.     /* ------------------ Modifier le contact ---------------- */
  53.     /**
  54.      * @Route("/contact/edit/{id}", name="app_contact_edit")
  55.      */
  56.     public function articleEdit(Contact $contactModifRequest $request): Response
  57.     {
  58.         $contactModif $this->createForm(ContactType::class, $contactModif);
  59.         // Traitement du formulaire
  60.         $contactModif->handleRequest($request);
  61.         // Si le formulaire est soumis et validé , alors...
  62.         if ($contactModif->isSubmitted() && $contactModif->isValid()) {
  63.             //$contactModif->setPublication(new \datetime);
  64.             // On persiste le contact modifié
  65.             $this->manager->persist($contactModif);
  66.             // On le flush 
  67.             $this->manager->flush();
  68.             // On redirige ensuite à la page qui liste les contacts
  69.             return $this->redirectToRoute('app_all_contacts');
  70.         }
  71.         return $this->render('contact/editContact.html.twig', [
  72.             // Matérialise l'affichage du formulaire
  73.             'myEditContact' => $contactModif->createView(),
  74.         ]);
  75.     }
  76.     /* ------------ Recherche et trouve tous les contacts ------------ */
  77.     /**
  78.      * @Route("/all/contact", name="app_all_contacts")
  79.      */
  80.     public function allContacts(): Response
  81.     {
  82.         $contacts $this->manager->getRepository(Contact::class)->findAll();
  83.         // Verifier que la variable a bien recu les données cherchés dans la BDD
  84.         // dd($contacts);
  85.         return $this->render('contact/allContacts.html.twig', [
  86.             // Matérialise l'affichage du formulaire
  87.             'contacts' => $contacts,
  88.         ]);
  89.     }
  90. }