<?php
namespace App\Controller;
use App\Entity\Blog;
use App\Entity\BlogAuthor;
use App\Entity\BlogTag;
use App\Repository\BlogAuthorRepository;
use App\Repository\BlogRepository;
use App\Repository\BlogTagRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/blog')]
class BlogController extends AbstractController
{
private const POSTS_PER_PAGE = 9;
#[Route('', name: 'app_blog_index', methods: ['GET'])]
public function index(Request $request, BlogRepository $blogRepository, BlogTagRepository $tagRepository): Response
{
$page = max(1, $request->query->getInt('page', 1));
$offset = ($page - 1) * self::POSTS_PER_PAGE;
$posts = $blogRepository->findPublished(self::POSTS_PER_PAGE, $offset);
$totalPosts = $blogRepository->countPublished();
$tags = $tagRepository->findTagsWithPostCounts();
return $this->render('blog/index.html.twig', [
'posts' => $posts,
'tags' => $tags,
'currentPage' => $page,
'totalPages' => ceil($totalPosts / self::POSTS_PER_PAGE),
'totalPosts' => $totalPosts,
]);
}
#[Route('/tag/{slug}', name: 'app_blog_tag', methods: ['GET'])]
public function tag(
Request $request,
string $slug,
BlogRepository $blogRepository,
BlogTagRepository $tagRepository
): Response {
$tag = $tagRepository->findOneBySlug($slug);
if (!$tag) {
throw $this->createNotFoundException('The tag does not exist.');
}
$page = max(1, $request->query->getInt('page', 1));
$offset = ($page - 1) * self::POSTS_PER_PAGE;
$posts = $blogRepository->findPublishedByTag($tag, self::POSTS_PER_PAGE, $offset);
$totalPosts = $blogRepository->countPublishedByTag($tag);
$tags = $tagRepository->findTagsWithPostCounts();
return $this->render('blog/tag.html.twig', [
'tag' => $tag,
'posts' => $posts,
'tags' => $tags,
'currentPage' => $page,
'totalPages' => ceil($totalPosts / self::POSTS_PER_PAGE),
'totalPosts' => $totalPosts,
]);
}
#[Route('/author/{id}', name: 'app_blog_author', methods: ['GET'])]
public function author(
Request $request,
int $id,
BlogRepository $blogRepository,
BlogTagRepository $tagRepository,
BlogAuthorRepository $blogAuthorRepository
): Response {
$author = $blogAuthorRepository->find($id);
if (!$author) {
throw $this->createNotFoundException('The author does not exist.');
}
$page = max(1, $request->query->getInt('page', 1));
$offset = ($page - 1) * self::POSTS_PER_PAGE;
$posts = $blogRepository->findPublishedByAuthor($author, self::POSTS_PER_PAGE, $offset);
$totalPosts = $blogRepository->countPublishedByAuthor($author);
$tags = $tagRepository->findTagsWithPostCounts();
return $this->render('blog/author.html.twig', [
'author' => $author,
'posts' => $posts,
'tags' => $tags,
'currentPage' => $page,
'totalPages' => ceil($totalPosts / self::POSTS_PER_PAGE),
'totalPosts' => $totalPosts,
]);
}
#[Route('/search', name: 'app_blog_search', methods: ['GET'])]
public function search(
Request $request,
BlogRepository $blogRepository,
BlogTagRepository $tagRepository
): Response {
$query = $request->query->get('q', '');
if (empty($query)) {
return $this->redirectToRoute('app_blog_index');
}
$posts = $blogRepository->search($query);
$tags = $tagRepository->findTagsWithPostCounts();
return $this->render('blog/search.html.twig', [
'query' => $query,
'posts' => $posts,
'tags' => $tags,
'totalPosts' => count($posts),
]);
}
#[Route('/{slug}', name: 'app_blog_show', methods: ['GET'])]
public function show(
string $slug,
BlogRepository $blogRepository,
BlogTagRepository $tagRepository
): Response {
$blog = $blogRepository->findOnePublishedBySlug($slug);
if (!$blog) {
throw $this->createNotFoundException('The blog post does not exist or is not published yet.');
}
$relatedPosts = $blogRepository->findRelated($blog);
$tags = $tagRepository->findTagsWithPostCounts();
return $this->render('blog/show.html.twig', [
'blog' => $blog,
'relatedPosts' => $relatedPosts,
'tags' => $tags,
]);
}
}