src/Controller/CatchAllController.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Twig\Error\LoaderError;
  7. use App\Twig\ContentfulRuntime;
  8. /**
  9.  * Catch All Controller to handle Contentful "routes"
  10.  *
  11.  * @author Chris Jones <chris.jones@hawkeyeagency.com>
  12.  */
  13. class CatchAllController extends AbstractController
  14. {
  15.     /**
  16.      * Used for debugging.
  17.      *
  18.      * @Route("/contentful-test/{id}")
  19.      */
  20.     public function test(string $idContentfulRuntime $contentful): Response
  21.     {
  22.         // Do not respond in non-DEV environments
  23.         if ($this->getParameter('kernel.environment') !== 'dev') {
  24.             throw $this->createNotFoundException();
  25.         }
  26.         dd($contentful->getEntry($id));
  27.     }
  28.     /**
  29.      * Main "catch-all" route for any page.
  30.      *
  31.      * @Route("/{slug}", defaults={"slug"="home"}, requirements={"slug"=".+"})
  32.      */
  33.     public function index(string $slug): Response
  34.     {
  35.         // Remove any trailing slash
  36.         $slug rtrim($slug'/');
  37.         if(strpos($slug".pdf") !== false ){
  38.             return new Response();
  39.         }
  40.         if ($slug == 'dementia-related-hallucinations-delusions') {
  41.             $slug 'dementia-related-hallucinations-delusions/home';
  42.         }
  43.         if ($slug == 'parkinsons-related-hallucinations-delusions') {
  44.             $slug 'parkinsons-related-hallucinations-delusions/home';
  45.         }
  46.         try {
  47.             $response $this->render("{$slug}.html.twig", []);
  48.         } catch (LoaderError $e) {
  49.             if ($this->getParameter('kernel.environment') === 'dev') {
  50.                 throw $this->createNotFoundException($e->getMessage());
  51.             }
  52.             $response $this->render("404-page-not-found.html.twig", []);
  53.             $response->setStatusCode(404);
  54.         }
  55.         // Cache publicly for 3600 seconds
  56.         $response->setPublic();
  57.         $response->setMaxAge(3600);
  58.         // (optional) Set a custom Cache-Control directive
  59.         $response->headers->addCacheControlDirective('must-revalidate'true);
  60.         return $response;
  61.     }
  62. }