src/Controller/DepartmentController.php line 56

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