Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controller;
- use Psr\Http\Message\ServerRequestInterface as Request;
- use Psr\Http\Message\ResponseInterface as Response;
- use App\Model\Car;
- final class HomeController extends BaseController
- {
- public function example(Request $request, Response $response, $args)
- {
- $em = $this->em;
- $page = ($request->getParam('page', 0) > 0) ? $request->getParam('page') : 1;
- $limit = 3; // Number of posts on one page
- $offset = ($page - 1) * $limit;
- $count = count($em->getRepository('App\Model\Car')->findAll()); // Count of all available posts
- return $this->view->render($response, 'example.twig', [
- 'pagination' => [
- 'needed' => $count > $limit,
- 'count' => $count,
- 'page' => $page,
- 'lastpage' => (ceil($count / $limit) == 0 ? 1 : ceil($count / $limit)),
- 'limit' => $limit,
- ],
- // return list of Posts with Limit and Skip arguments
- 'posts' => $em->getRepository('App\Model\Car')->findBy([], null, $limit, $offset)
- ]);
- }
- }
- {% extends 'base.twig' %}
- {% block title %}Page exemple{% endblock %}
- {% block body %}
- <h1>Formulaire d'ajout</h1>
- {% if pagination.needed %}
- <div class="ui pagination menu">
- {% for i in 1..pagination.lastpage %}
- <a class="{% if i == pagination.page %}active{% endif %} item" href="?page={{ i }}">{{ i }}</a>
- {% endfor %}
- </div>
- {% endif %}
- <div class="ui container">
- {% for post in posts %}
- <a class="item">
- {# Post contents (title, url, ...) #}
- {{ dump(post.name) }}
- </a>
- {% endfor %}
- </div>
- {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement