src/Controller/CityController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\City;
  4. use App\Form\CityType;
  5. use App\Repository\CityRepository;
  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("/city")
  12. */
  13. class CityController extends AbstractController
  14. {
  15. /**
  16. * @Route("/", name="city_index", methods={"GET"})
  17. */
  18. public function index(CityRepository $cityRepository): Response
  19. {
  20. return $this->render('city/index.html.twig', [
  21. 'cities' => $cityRepository->findAll(),
  22. ]);
  23. }
  24. /**
  25. * @Route("/new", name="city_new", methods={"GET","POST"})
  26. */
  27. public function new(Request $request): Response
  28. {
  29. $city = new City();
  30. $form = $this->createForm(CityType::class, $city);
  31. $form->handleRequest($request);
  32. if ($form->isSubmitted() && $form->isValid()) {
  33. $entityManager = $this->getDoctrine()->getManager();
  34. $entityManager->persist($city);
  35. $entityManager->flush();
  36. return $this->redirectToRoute('city_index');
  37. }
  38. return $this->render('city/new.html.twig', [
  39. 'city' => $city,
  40. 'form' => $form->createView(),
  41. ]);
  42. }
  43. /**
  44. * @Route("/{id}", name="city_show", methods={"GET"})
  45. */
  46. public function show(City $city): Response
  47. {
  48. return $this->render('city/show.html.twig', [
  49. 'city' => $city,
  50. ]);
  51. }
  52. /**
  53. * @Route("/{id}/edit", name="city_edit", methods={"GET","POST"})
  54. */
  55. public function edit(Request $request, City $city): Response
  56. {
  57. $form = $this->createForm(CityType::class, $city);
  58. $form->handleRequest($request);
  59. if ($form->isSubmitted() && $form->isValid()) {
  60. $this->getDoctrine()->getManager()->flush();
  61. return $this->redirectToRoute('city_index');
  62. }
  63. return $this->render('city/edit.html.twig', [
  64. 'city' => $city,
  65. 'form' => $form->createView(),
  66. ]);
  67. }
  68. /**
  69. * @Route("/{id}", name="city_delete", methods={"DELETE"})
  70. */
  71. public function delete(Request $request, City $city): Response
  72. {
  73. if ($this->isCsrfTokenValid('delete'.$city->getId(), $request->request->get('_token'))) {
  74. $entityManager = $this->getDoctrine()->getManager();
  75. $entityManager->remove($city);
  76. $entityManager->flush();
  77. }
  78. return $this->redirectToRoute('city_index');
  79. }
  80. }