Style pagination with Bootstrap
This commit is contained in:
@@ -6,20 +6,22 @@ use App\Repository\PostRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class PostController extends AbstractController
|
||||
{
|
||||
private const POSTS_PER_PAGE = 10;
|
||||
|
||||
#[Route('/', name: 'app_posts')]
|
||||
public function index(PostRepository $repository, Request $request): Response
|
||||
{
|
||||
$page = $request->query->getInt('page',1);
|
||||
$posts = $repository->findPaginatedPosts($page, 10);
|
||||
$page = $request->query->getInt('page', 1);
|
||||
$posts = $repository->findPaginatedPosts($page, self::POSTS_PER_PAGE);
|
||||
$maxPage = ceil($posts->count() / self::POSTS_PER_PAGE);
|
||||
return $this->render('post/index.html.twig', [
|
||||
'posts' => $posts,
|
||||
'maxPosts' => $page * 10,
|
||||
'currentPage' => $page
|
||||
'maxPage' => $maxPage,
|
||||
'page' => $page,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -17,6 +17,11 @@ class PostRepository extends ServiceEntityRepository
|
||||
parent::__construct($registry, Post::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
* @param int $limit
|
||||
* @return Paginator<Post>
|
||||
*/
|
||||
public function findPaginatedPosts(int $page, int $limit): Paginator
|
||||
{
|
||||
$query = $this->createQueryBuilder('p')
|
||||
@@ -25,6 +30,7 @@ class PostRepository extends ServiceEntityRepository
|
||||
|
||||
return new Paginator($query, fetchJoinCollection: false);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Post[] Returns an array of Post objects
|
||||
// */
|
||||
|
Reference in New Issue
Block a user