From 89530fd6e077dc061f2a5bbb20d9eb502b37b2c6 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Fri, 23 Nov 2018 23:22:17 +0100 Subject: [PATCH] Optimized Imprint function --- .../Controller/AbstractEntityController.php | 12 ++++++++++++ .../src/Controller/DefaultController.php | 18 +++++++++++++++--- .../templates/standard/imprint.html.twig | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/application/src/Controller/AbstractEntityController.php b/application/src/Controller/AbstractEntityController.php index 187936d..0804b86 100644 --- a/application/src/Controller/AbstractEntityController.php +++ b/application/src/Controller/AbstractEntityController.php @@ -35,6 +35,18 @@ abstract class AbstractEntityController extends FOSRestController return $entity; } + protected function loadEntityBySlug(string $slug): EntityInterface + { + $entity = $this->getDoctrine() + ->getRepository($this->entityName) + ->findOneBy(['slug' => $slug]); + if (!$entity) { + throw $this->createNotFoundException('No entity found for slug '.$slug); + } + + return $entity; + } + protected function redirectToRouteById(string $route, int $id): RedirectResponse { return $this->redirectToRoute($route, [ diff --git a/application/src/Controller/DefaultController.php b/application/src/Controller/DefaultController.php index ec153e0..f5b3587 100644 --- a/application/src/Controller/DefaultController.php +++ b/application/src/Controller/DefaultController.php @@ -2,21 +2,28 @@ namespace App\Controller; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\HttpFoundation\Response; +use App\DBAL\Types\SystemSlugType; +use App\Entity\Source\AbstractSource; /** * @author kevinfrantz */ -class DefaultController extends AbstractController +class DefaultController extends AbstractEntityController { /** + * @todo Optimize function! * @Route("/imprint", name="imprint") */ public function imprint(): Response { - return $this->render('standard/imprint.html.twig'); + $source = $this->loadEntityBySlug(SystemSlugType::IMPRINT); + $view = $this->view($source, 200) + ->setTemplate('standard/imprint.html.twig') + ->setTemplateVar('source'); + + return $this->handleView($view); } /** @@ -26,4 +33,9 @@ class DefaultController extends AbstractController { return $this->render('standard/homepage.html.twig'); } + + protected function setEntityName(): void + { + $this->entityName = AbstractSource::class; + } } diff --git a/application/templates/standard/imprint.html.twig b/application/templates/standard/imprint.html.twig index be73a95..66de32d 100644 --- a/application/templates/standard/imprint.html.twig +++ b/application/templates/standard/imprint.html.twig @@ -8,5 +8,5 @@ Homepage Imprint {% endtrans %} -Developed by kevinfrantz +{{ source.text }} {% endblock %}