<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Error\LoaderError;
use App\Twig\ContentfulRuntime;
/**
* Catch All Controller to handle Contentful "routes"
*
* @author Chris Jones <chris.jones@hawkeyeagency.com>
*/
class CatchAllController extends AbstractController
{
/**
* Used for debugging.
*
* @Route("/contentful-test/{id}")
*/
public function test(string $id, ContentfulRuntime $contentful): Response
{
// Do not respond in non-DEV environments
if ($this->getParameter('kernel.environment') !== 'dev') {
throw $this->createNotFoundException();
}
dd($contentful->getEntry($id));
}
/**
* Main "catch-all" route for any page.
*
* @Route("/{slug}", defaults={"slug"="home"}, requirements={"slug"=".+"})
*/
public function index(string $slug): Response
{
// Remove any trailing slash
$slug = rtrim($slug, '/');
if(strpos($slug, ".pdf") !== false ){
return new Response();
}
if ($slug == 'dementia-related-hallucinations-delusions') {
$slug = 'dementia-related-hallucinations-delusions/home';
}
if ($slug == 'parkinsons-related-hallucinations-delusions') {
$slug = 'parkinsons-related-hallucinations-delusions/home';
}
try {
$response = $this->render("{$slug}.html.twig", []);
} catch (LoaderError $e) {
if ($this->getParameter('kernel.environment') === 'dev') {
throw $this->createNotFoundException($e->getMessage());
}
$response = $this->render("404-page-not-found.html.twig", []);
$response->setStatusCode(404);
}
// Cache publicly for 3600 seconds
$response->setPublic();
$response->setMaxAge(3600);
// (optional) Set a custom Cache-Control directive
$response->headers->addCacheControlDirective('must-revalidate', true);
return $response;
}
}