1
0
Fork 0

Pagination

This commit is contained in:
Ethanell 2021-01-14 16:45:08 +01:00
parent ffc9555132
commit 9c23282b59
3 changed files with 18 additions and 4 deletions

View file

@ -10,13 +10,18 @@ use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
/**
* @Route("/", name="home")
* @Route("/{page}", name="home")
* @param int $page
* @return Response
*/
public function index(): Response
public function index(int $page = 0): Response
{
$repo = $this->getDoctrine()->getRepository(Post::class);
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
'posts' => $this->getDoctrine()->getRepository(Post::class)->findBy(array(), array('publishedAt' => 'DESC'), 5, 0)
'posts' => $repo->findBy(array(), array('publishedAt' => 'DESC'), 5, $page*5),
'pages' => round($repo->count(array())/5, 0, PHP_ROUND_HALF_UP),
'page' => $page
]);
}
}

View file

@ -9,7 +9,6 @@
{% set admin = false %} <!-- ToDo: Change this with dynamic -->
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="{{ path('home') }}">Blog</a>

View file

@ -16,4 +16,14 @@
</div>
{% endfor %}
</div>
<nav aria-label="Post navigation">
<ul class="pagination justify-content-center">
<li class="page-item {{ page <= 0 ? "disabled" : "" }}"><a class="page-link" href="{{ path('home', {'page': page-1}) }}">Previous</a></li>
{% for p in 0..pages %}
{% set active = p == page %}
<li class="page-item {{ active ? 'active' : '' }} {{ active ? '' : 'aria-current="page"' }}"><a class="page-link" href="{{ path('home', {'page': p}) }}">{{ p }} {{ active ? '<span class="visually-hidden">(current)</span>' : '' }}</a></li>
{% endfor %}
<li class="page-item {{ page >= pages ? "disabled" : "" }}"><a class="page-link" href="{{ path('home', {'page': page+1}) }}">Next</a></li>
</ul>
</nav>
{% endblock %}