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\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  11. class ContactController extends AbstractController
  12. {
  13.    public function __construct(EntityManagerInterface $managerFlashBagInterface $flash){
  14.        $this->manager $manager;
  15.        $this->flash $flash;
  16.    }
  17.    
  18.     /**
  19.      * @Route("/contact", name="app_contact")
  20.      */
  21.     public function index(Request $request): Response
  22.     {
  23.         $contact = new Contact();
  24.         $form $this->createForm(ContactType::class, $contact);
  25.         $form->handleRequest($request);
  26.         if($form->isSubmitted() && $form->isValid()) {
  27.             $this->manager->persist($contact); // On persister l'utilisateur (cela signifie elle prépare l'envoie des données)
  28.             $this->manager->flush(); // On flush
  29.             $this->flash->add('success''Votre message a bien été envoyé');
  30.             return $this->redirectToRoute('app_contact'); // on redirige vers la page de connexion
  31.         }
  32.         return $this->render('contact/index.html.twig', [
  33.             'myForm' => $form->createView()
  34.         ]);
  35.         
  36.     }
  37.      /**
  38.      * @Route("/admin/all/contact", name="app_admin_contact")
  39.      */
  40.     public function toutlesmessage(): Response
  41.     {
  42.        
  43.         $message $this->manager->getRepository(Contact::class)->findAll();
  44.         return $this->render('contact/messagerie.html.twig', [
  45.             'message' => $message,
  46.         ]);
  47.     }
  48.     /**
  49.      * @Route("/admin/delete/contact{id}", name="app_admin_delete_contact")
  50.      */
  51.     public function delete(Contact $contact): Response
  52.     {
  53.        $this->manager->remove($contact);
  54.        $this->manager->flush();
  55.        return $this->redirectToRoute('app_admin_contact');
  56.     }
  57. }