Secure pagination
This commit is contained in:
parent
e63473e5c7
commit
cfe7d9339c
1 changed files with 7 additions and 1 deletions
|
@ -23,9 +23,15 @@ class PostController extends AbstractController
|
||||||
public function index(int $page = 0): Response
|
public function index(int $page = 0): Response
|
||||||
{
|
{
|
||||||
$repo = $this->getDoctrine()->getRepository(Post::class);
|
$repo = $this->getDoctrine()->getRepository(Post::class);
|
||||||
|
$pages = (int) round(count($repo->getPublished())/5, 0, PHP_ROUND_HALF_UP);
|
||||||
|
if ($page < 0) {
|
||||||
|
$page = 0;
|
||||||
|
} else if ($page > $pages) {
|
||||||
|
$page = $pages;
|
||||||
|
}
|
||||||
return $this->render('home/index.html.twig', [
|
return $this->render('home/index.html.twig', [
|
||||||
'posts' => $repo->getPublished($page*5, 5),
|
'posts' => $repo->getPublished($page*5, 5),
|
||||||
'pages' => round(count($repo->getPublished())/5, 0, PHP_ROUND_HALF_UP)-1,
|
'pages' => $pages-1,
|
||||||
'page' => $page
|
'page' => $page
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue