feat(controller): add details controller
This commit is contained in:
@@ -1,8 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
class DetailPostController
|
||||
{
|
||||
use App\Repository\Post\PostRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class DetailPostController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PostRepository $postRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route('/posts/detail/{postId}', name: 'detail_post', requirements: ['postId' => '\d+'])]
|
||||
public function detail(int $postId): Response
|
||||
{
|
||||
$post = $this->postRepository->find($postId);
|
||||
|
||||
if ($post === null) {
|
||||
throw new NotFoundHttpException('Post not found. Please check the provided ID. If the problem persists, contact the system administrator');
|
||||
}
|
||||
|
||||
return $this->render(
|
||||
'detail_post/detail.html.twig',
|
||||
['post' => $post]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace App\Controller;
|
||||
|
||||
use App\Repository\Post\PostRepository;
|
||||
use App\Utils\Paginator\Paginator;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
@@ -18,6 +19,7 @@ final class ListPostController extends AbstractController
|
||||
private readonly int $maxVisiblePages,
|
||||
private readonly PostRepository $postRepository,
|
||||
private readonly Paginator $paginator,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -38,11 +40,13 @@ final class ListPostController extends AbstractController
|
||||
}
|
||||
|
||||
if ($page === 0) {
|
||||
$this->logger->error('Page number is zero. Redirecting to the first page.');
|
||||
return $this->redirectToRoute('page_list', ['page' => 1]);
|
||||
}
|
||||
|
||||
// If page number is out of range, redirect to the last page.
|
||||
if ($page > $totalPages) {
|
||||
$this->logger->error(sprintf('Page number %d is out of range. Redirecting to the last page: %d', $page, $totalPages));
|
||||
return $this->redirectToRoute('page_list', ['page' => $totalPages]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user