src/Controller/BlogController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\ArticleRepository;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. class BlogController extends AbstractController
  9. {
  10. private const PER_PAGE = 6;
  11. /**
  12. * @Route("/actualites", name="blog_index")
  13. */
  14. public function index(Request $request, ArticleRepository $articleRepository, \App\Repository\ExpertiseRepository $expertiseRepository): Response
  15. {
  16. $page = max(1, (int) $request->query->get('page', 1));
  17. $paginator = $articleRepository->findPaginated($page, self::PER_PAGE);
  18. $totalArticles = count($paginator);
  19. $totalPages = (int) ceil($totalArticles / self::PER_PAGE);
  20. return $this->render('blog/index.html.twig', [
  21. 'articles' => $paginator,
  22. 'recentArticles' => $articleRepository->findBy([], ['createdAt' => 'DESC'], 3),
  23. 'currentPage' => $page,
  24. 'totalPages' => $totalPages,
  25. 'expertises' => $expertiseRepository->findAll(),
  26. ]);
  27. }
  28. /**
  29. * @Route("/actualites/{slug}", name="blog_detail")
  30. */
  31. public function detail(string $slug, ArticleRepository $articleRepository, \App\Repository\ExpertiseRepository $expertiseRepository): Response
  32. {
  33. $article = $articleRepository->findOneBy(['slug' => $slug]);
  34. if (!$article) {
  35. throw $this->createNotFoundException('Article non trouvé.');
  36. }
  37. return $this->render('blog/detail.html.twig', [
  38. 'article' => $article,
  39. 'recentArticles' => $articleRepository->findBy([], ['createdAt' => 'DESC'], 3),
  40. 'expertises' => $expertiseRepository->findAll(),
  41. ]);
  42. }
  43. }