Pagination
This commit is contained in:
parent
ffc9555132
commit
9c23282b59
3 changed files with 18 additions and 4 deletions
|
@ -10,13 +10,18 @@ use Symfony\Component\Routing\Annotation\Route;
|
||||||
class HomeController extends AbstractController
|
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', [
|
return $this->render('home/index.html.twig', [
|
||||||
'controller_name' => 'HomeController',
|
'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
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
{% set admin = false %} <!-- ToDo: Change this with dynamic -->
|
{% set admin = false %} <!-- ToDo: Change this with dynamic -->
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<!-- Navbar -->
|
|
||||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
<a class="navbar-brand" href="{{ path('home') }}">Blog</a>
|
<a class="navbar-brand" href="{{ path('home') }}">Blog</a>
|
||||||
|
|
|
@ -16,4 +16,14 @@
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</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 %}
|
{% endblock %}
|
||||||
|
|
Reference in a new issue