<?php
namespace Kobizo\Bundle\CmsBundle\Controller;
use Kobizo\Bundle\CmsBundle\Controller\Page\FeObjectHelper;
use Kobizo\Bundle\CmsBundle\Controller\Page\GetPageTranslationHelper;
use Kobizo\Bundle\CmsBundle\Controller\ViewHelper\PageViewHelper;
use Kobizo\Component\Attributes\Type\PageTypesAttribute;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class FePageController extends KobizoFeAbstractController
{
/**
* @Route("/%kobizo.slug.page%/{slug}", name="display_a_page", methods={"GET"})
* @Route("/{slug}", name="display_a_page_short", methods={"GET"})
*/
public function showPageBySlug(
Request $request,
string $slug,
GetPageTranslationHelper $getPageTranslationHelper,
FeObjectHelper $feObjectHelper,
PageViewHelper $pageViewHelper
): Response {
$locale = $request->getLocale();
$pageTranslation = $getPageTranslationHelper->getByLocaleAndSlug($locale, $slug);
if (!$pageTranslation) {
throw new NotFoundHttpException('No page found.');
}
if ($pageTranslation->getSlug() !== $slug) {
return $this->redirectToRoute(
$request->get('_route'),
[
'_locale' => $pageTranslation->getLanguageCode(),
'slug' => $pageTranslation->getSlug(),
]
);
}
$page = $pageTranslation->getPage();
if (($position = strpos($page->getType(), PageTypesAttribute::HIDDEN)) !== false && $position === 0) {
throw new NotFoundHttpException('No page found.');
}
return $this->render(
$pageViewHelper->getView($page, '@KobizoCms/frontend/page.twig'),
$feObjectHelper->getObjectArray($locale, $page, $pageTranslation)
);
}
}