src/Controller/BlogController.php line 128

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Blog;
  4. use App\Entity\BlogAuthor;
  5. use App\Entity\BlogTag;
  6. use App\Repository\BlogAuthorRepository;
  7. use App\Repository\BlogRepository;
  8. use App\Repository\BlogTagRepository;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. #[Route('/blog')]
  14. class BlogController extends AbstractController
  15. {
  16.     private const POSTS_PER_PAGE 9;
  17.     #[Route(''name'app_blog_index'methods: ['GET'])]
  18.     public function index(Request $requestBlogRepository $blogRepositoryBlogTagRepository $tagRepository): Response
  19.     {
  20.         $page max(1$request->query->getInt('page'1));
  21.         $offset = ($page 1) * self::POSTS_PER_PAGE;
  22.         
  23.         $posts $blogRepository->findPublished(self::POSTS_PER_PAGE$offset);
  24.         $totalPosts $blogRepository->countPublished();
  25.         
  26.         $tags $tagRepository->findTagsWithPostCounts();
  27.         
  28.         return $this->render('blog/index.html.twig', [
  29.             'posts' => $posts,
  30.             'tags' => $tags,
  31.             'currentPage' => $page,
  32.             'totalPages' => ceil($totalPosts self::POSTS_PER_PAGE),
  33.             'totalPosts' => $totalPosts,
  34.         ]);
  35.     }
  36.     #[Route('/tag/{slug}'name'app_blog_tag'methods: ['GET'])]
  37.     public function tag(
  38.         Request $request,
  39.         string $slug,
  40.         BlogRepository $blogRepository,
  41.         BlogTagRepository $tagRepository
  42.     ): Response {
  43.         $tag $tagRepository->findOneBySlug($slug);
  44.         if (!$tag) {
  45.             throw $this->createNotFoundException('The tag does not exist.');
  46.         }
  47.         $page max(1$request->query->getInt('page'1));
  48.         $offset = ($page 1) * self::POSTS_PER_PAGE;
  49.         
  50.         $posts $blogRepository->findPublishedByTag($tagself::POSTS_PER_PAGE$offset);
  51.         $totalPosts $blogRepository->countPublishedByTag($tag);
  52.         
  53.         $tags $tagRepository->findTagsWithPostCounts();
  54.         
  55.         return $this->render('blog/tag.html.twig', [
  56.             'tag' => $tag,
  57.             'posts' => $posts,
  58.             'tags' => $tags,
  59.             'currentPage' => $page,
  60.             'totalPages' => ceil($totalPosts self::POSTS_PER_PAGE),
  61.             'totalPosts' => $totalPosts,
  62.         ]);
  63.     }
  64.     #[Route('/author/{id}'name'app_blog_author'methods: ['GET'])]
  65.     public function author(
  66.         Request $request,
  67.         int $id,
  68.         BlogRepository $blogRepository,
  69.         BlogTagRepository $tagRepository,
  70.         BlogAuthorRepository $blogAuthorRepository
  71.     ): Response {
  72.         $author $blogAuthorRepository->find($id);
  73.         if (!$author) {
  74.             throw $this->createNotFoundException('The author does not exist.');
  75.         }
  76.         $page max(1$request->query->getInt('page'1));
  77.         $offset = ($page 1) * self::POSTS_PER_PAGE;
  78.         
  79.         $posts $blogRepository->findPublishedByAuthor($authorself::POSTS_PER_PAGE$offset);
  80.         $totalPosts $blogRepository->countPublishedByAuthor($author);
  81.         
  82.         $tags $tagRepository->findTagsWithPostCounts();
  83.         
  84.         return $this->render('blog/author.html.twig', [
  85.             'author' => $author,
  86.             'posts' => $posts,
  87.             'tags' => $tags,
  88.             'currentPage' => $page,
  89.             'totalPages' => ceil($totalPosts self::POSTS_PER_PAGE),
  90.             'totalPosts' => $totalPosts,
  91.         ]);
  92.     }
  93.     #[Route('/search'name'app_blog_search'methods: ['GET'])]
  94.     public function search(
  95.         Request $request,
  96.         BlogRepository $blogRepository,
  97.         BlogTagRepository $tagRepository
  98.     ): Response {
  99.         $query $request->query->get('q''');
  100.         
  101.         if (empty($query)) {
  102.             return $this->redirectToRoute('app_blog_index');
  103.         }
  104.         
  105.         $posts $blogRepository->search($query);
  106.         $tags $tagRepository->findTagsWithPostCounts();
  107.         
  108.         return $this->render('blog/search.html.twig', [
  109.             'query' => $query,
  110.             'posts' => $posts,
  111.             'tags' => $tags,
  112.             'totalPosts' => count($posts),
  113.         ]);
  114.     }
  115.     #[Route('/{slug}'name'app_blog_show'methods: ['GET'])]
  116.     public function show(
  117.         string $slug,
  118.         BlogRepository $blogRepository,
  119.         BlogTagRepository $tagRepository
  120.     ): Response {
  121.         $blog $blogRepository->findOnePublishedBySlug($slug);
  122.         
  123.         if (!$blog) {
  124.             throw $this->createNotFoundException('The blog post does not exist or is not published yet.');
  125.         }
  126.         
  127.         $relatedPosts $blogRepository->findRelated($blog);
  128.         $tags $tagRepository->findTagsWithPostCounts();
  129.         
  130.         return $this->render('blog/show.html.twig', [
  131.             'blog' => $blog,
  132.             'relatedPosts' => $relatedPosts,
  133.             'tags' => $tags,
  134.         ]);
  135.     }
  136. }