src/Controller/ContactController.php line 31

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