src/Controller/Content/App/PageController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Content\App;
  3. use App\Entity\Content\Page;
  4. use App\Entity\Content\PageTranslation;
  5. use App\Repository\Content\PageRepository;
  6. use App\Service\Content\Widgetizer;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration as Ctrl;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. /**
  13. * @Route("/", requirements={"_locale": "\w{2}"})
  14. */
  15. class PageController extends AbstractController
  16. {
  17. public function __construct(protected readonly Widgetizer $widgetizer)
  18. {
  19. }
  20. /**
  21. * Finds and displays a page entity.
  22. *
  23. * @Route("/{_locale}/", name="mdl_public_page_index", methods={"GET"})
  24. * @Ctrl\Template("Content/Public/Page/show.html.twig")
  25. * @Ctrl\Cache(expires="+2 days", public=true)
  26. */
  27. public function indexAction(Request $request, PageRepository $pageRepository): Response
  28. {
  29. $page = $pageRepository->findIndex();
  30. /** @var PageTranslation $translation */
  31. $translation = $page->translate($request->getLocale());
  32. return $this->render('Content/Public/index.html.twig', [
  33. 'translation' => $this->widgetizer->widgetize($translation),
  34. ]);
  35. }
  36. /**
  37. * Finds and displays a page entity.
  38. * @Route("/404", name="mdl_public_page_not_found", methods={"GET"})
  39. * @Ctrl\Cache(expires="+2 days", public=true)
  40. */
  41. public function notFoundAction(): Response
  42. {
  43. $page = new Page();
  44. $translation = new PageTranslation();
  45. $translation->setContent('<b>Error 404</b> Page not found')
  46. ->setTitle('Page not found')
  47. ->setSlug('page-not-found');
  48. $page->addTranslation($translation);
  49. return $this->render('Content/Public/Page/show.html.twig', [
  50. 'translation' => $page
  51. ]);
  52. }
  53. /**
  54. * Finds and displays a page entity.
  55. * @Route(
  56. * "/{_locale}/{slug}",
  57. * name="mdl_public_page_show",
  58. * methods={"GET"},
  59. * requirements={"_locale":"\w{2}", "slug": "[\w-]+"}
  60. * )
  61. * @Ctrl\ParamConverter("translation",
  62. * class="App\Entity\Content\PageTranslation",
  63. * options={
  64. * "repository_method": "findOneBySlug",
  65. * "mapping": {
  66. * "_locale": "locale",
  67. * "slug": "slug",
  68. * },
  69. * "map_method_signature": true
  70. * }
  71. * )
  72. * @Ctrl\Cache(
  73. * Etag="pageTranslation.getLocale() ~ pageTranslation.getId() ~ pageTranslation.getTranslatable().getUpdatedAt().getTimestamp()",
  74. * expires="+2 days",
  75. * lastModified="pageTranslation.getTranslatable().getUpdatedAt()",
  76. * public=true
  77. * )
  78. */
  79. public function showAction(PageTranslation $pageTranslation): Response
  80. {
  81. return $this->render('Content/Public/Page/show.html.twig', [
  82. 'translation' => $this->widgetizer->widgetize($pageTranslation),
  83. ]);
  84. }
  85. }