<?php
namespace MLDev\BaseBundle\Controller;
use Doctrine\ORM\UnexpectedResultException;
use MLDev\BaseBundle\Entity\PageTemplate;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use MLDev\BaseBundle\Entity\Page;
use MLDev\BaseBundle\Repository\PageRepository;
/**
* Class FrontController
* @package MLDev\BaseBundle\Controller
*/
class FrontController extends AbstractController
{
/**
* @Route("/{uri}", name="mldev-front-show", requirements={"uri": "^.*$"}, priority="-9999")
*/
public function showByUri(PageRepository $pageRepository, Request $request, ?string $uri = null): Response
{
try {
$entity = $pageRepository->getPageByUri($uri);
} catch (UnexpectedResultException $exception) {
throw $this->createNotFoundException('The page does not exist');
}
return $this->renderLayout($entity, $this->getTemplate($entity));
}
protected function renderCustomTemplate(Page $entity, string $template, array $parameters = []): Response
{
return $this->renderLayout($entity, $template, null, $parameters);
}
protected function renderLayout(Page $entity, string $template, ?string $layout = null, $parameters = []): Response
{
$layout = $layout ?? $this->getLayoutTemplate($entity);
return $this->render($layout, [
'page' => $entity,
'CONTENT' => $this->renderContentZone($entity, $template, $parameters),
]);
}
private function renderContentZone(Page $entity, string $template, array $parameters = []): string
{
return $this->renderView($template, array_merge([
'page' => $entity,
], $parameters));
}
private function getTemplate(Page $entity): string
{
return sprintf('@MLDevBase/templates/%s.html.twig', $entity->getTemplate()->getAlias());
}
private function getLayoutTemplate(Page $entity): string
{
return sprintf('@MLDevBase/layout/%s.html.twig', $entity->getLayoutTemplate()->getAlias());
}
}