<?php
namespace App\Controller;
use App\Repository\HeaderRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
class HeaderController extends AbstractController {
private $entityManager;
public function __construct(EntityManagerInterface $entityManager) {
$this->entityManager = $entityManager;
}
/**
* @Route("/articles/{slug}", name="app_header_articles")
*/
public function index($slug, HeaderRepository $headerRepo, CacheInterface $cache): Response {
$annonce = $cache->get('app_header_articles_' . $slug, function (ItemInterface $item) use ($headerRepo, $slug) {
$item->expiresAfter(20);
return $headerRepo->findOneBy(['slug' => $slug]);
});
if (!$annonce) {
throw new NotFoundHttpException('Pas d\'article trouvé');
}
return $this->render('header/index.html.twig', [
'annonces' => $annonce,
]);
}
}