PageController.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Page;
  4. use App\Form\PageType;
  5. use App\Repository\PageRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class PageController extends AbstractController
  12. {
  13. #[Route('/p/{id}', name: 'app_page_show', methods: ['GET'])]
  14. public function showPage(PageRepository $pageRepository, string $id): Response
  15. {
  16. $page = $pageRepository->find($id);
  17. if (!$page) {
  18. throw $this->createNotFoundException('La page ' . $id . ' n\'existe pas.');
  19. }
  20. return $this->render('page/index.html.twig', [
  21. 'page' => $page
  22. ]);
  23. }
  24. #[Route('/p/edit/{id}', name: 'app_page_edit', methods: ['GET', 'POST'])]
  25. public function editPage(Request $request, Page $page, EntityManagerInterface $em): Response
  26. {
  27. $form = $this->createForm(PageType::class, $page);
  28. $form->handleRequest($request);
  29. if ($form->isSubmitted() && $form->isValid()) {
  30. $em->flush();
  31. $this->addFlash('success', 'Contenu modifié');
  32. return $this->redirectToRoute('app_page_show', ['name' => $page->getName()]);
  33. }
  34. return $this->render('page/edit.html.twig', [
  35. 'title' => $page->getName(),
  36. 'form' => $form
  37. ]);
  38. }
  39. }