<?php
namespace App\Controller\Content\App;
use App\Entity\Content\Page;
use App\Entity\Content\PageTranslation;
use App\Repository\Content\PageRepository;
use App\Service\Content\Widgetizer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration as Ctrl;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* @Route("/", requirements={"_locale": "\w{2}"})
*/
class PageController extends AbstractController
{
public function __construct(protected readonly Widgetizer $widgetizer)
{
}
/**
* Finds and displays a page entity.
*
* @Route("/{_locale}/", name="mdl_public_page_index", methods={"GET"})
* @Ctrl\Template("Content/Public/Page/show.html.twig")
* @Ctrl\Cache(expires="+2 days", public=true)
*/
public function indexAction(Request $request, PageRepository $pageRepository): Response
{
$page = $pageRepository->findIndex();
/** @var PageTranslation $translation */
$translation = $page->translate($request->getLocale());
return $this->render('Content/Public/index.html.twig', [
'translation' => $this->widgetizer->widgetize($translation),
]);
}
/**
* Finds and displays a page entity.
* @Route("/404", name="mdl_public_page_not_found", methods={"GET"})
* @Ctrl\Cache(expires="+2 days", public=true)
*/
public function notFoundAction(): Response
{
$page = new Page();
$translation = new PageTranslation();
$translation->setContent('<b>Error 404</b> Page not found')
->setTitle('Page not found')
->setSlug('page-not-found');
$page->addTranslation($translation);
return $this->render('Content/Public/Page/show.html.twig', [
'translation' => $page
]);
}
/**
* Finds and displays a page entity.
* @Route(
* "/{_locale}/{slug}",
* name="mdl_public_page_show",
* methods={"GET"},
* requirements={"_locale":"\w{2}", "slug": "[\w-]+"}
* )
* @Ctrl\ParamConverter("translation",
* class="App\Entity\Content\PageTranslation",
* options={
* "repository_method": "findOneBySlug",
* "mapping": {
* "_locale": "locale",
* "slug": "slug",
* },
* "map_method_signature": true
* }
* )
* @Ctrl\Cache(
* Etag="pageTranslation.getLocale() ~ pageTranslation.getId() ~ pageTranslation.getTranslatable().getUpdatedAt().getTimestamp()",
* expires="+2 days",
* lastModified="pageTranslation.getTranslatable().getUpdatedAt()",
* public=true
* )
*/
public function showAction(PageTranslation $pageTranslation): Response
{
return $this->render('Content/Public/Page/show.html.twig', [
'translation' => $this->widgetizer->widgetize($pageTranslation),
]);
}
}