src/Controller/AchievementController.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Realisation;
  4. use App\Repository\CategorieRepository;
  5. use App\Repository\RealisationRepository;
  6. use Doctrine\ORM\NonUniqueResultException;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  12. use Symfony\Contracts\Cache\ItemInterface;
  13. class AchievementController extends AbstractController
  14. {
  15.     private $repository;
  16.     private $categorieRepository;
  17.     public function __construct(RealisationRepository $repositoryCategorieRepository $categorieRepository)
  18.     {
  19.         $this->repository $repository;
  20.         $this->categorieRepository $categorieRepository;
  21.     }
  22.     /**
  23.      * @Route("/realisations", name="achievements")
  24.      */
  25.     public function index(): Response
  26.     {
  27.         $cache = new FilesystemAdapter();
  28.         $realisations $cache->get('achievement-realisations', function (ItemInterface $item) {
  29.             $item->expiresAfter(600);
  30.             return $this->repository->findAll();
  31.         });
  32.         $categories $cache->get('achievement-categories', function (ItemInterface $item) {
  33.             $item->expiresAfter(3600);
  34.             return $this->categorieRepository->findAll();
  35.         });
  36.         $lasts $cache->get('achievement-lasts', function (ItemInterface $item) {
  37.             $item->expiresAfter(600);
  38.             return $this->repository->findLastOfCategory();
  39.         });
  40.         return $this->render('achievement/index.html.twig', [
  41.             'realisations' => $realisations,
  42.             'categories' => $categories,
  43.             'lasts' => $lasts,
  44.             'titre' => 'Nos réalisations',
  45.             'sousTitre' => 'découvrez',
  46.             'banner' => 'facade-maison.jpg',
  47.         ]);
  48.     }
  49.     /**
  50.      * @Route("/realisation/{slug}", name="realisation")
  51.      * @throws NonUniqueResultException
  52.      */
  53.     public function show($slug)
  54.     {
  55.         $realisation $this->repository->findOneBySlug($slug);
  56.         if (!$realisation){
  57.             return $this->redirectToRoute('realisations');
  58.         }
  59.         $realByCat $this->repository->findByCategory($realisation->getCategorie()->getId(), $realisation->getId());
  60. //        dd($realByCat);
  61.         return $this->render('achievement/show.html.twig', [
  62.             'realisation' => $realisation,
  63.             'others' => $realByCat,
  64.             'titre' => $realisation->getNom(),
  65.             'sousTitre' => 'nos réalisations',
  66.             'banner' => 'facade-maison.jpg',
  67.         ]);
  68.     }
  69. }