Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. <?php
  2. namespace App\Controller;
  3.  
  4. use Psr\Http\Message\ServerRequestInterface as Request;
  5. use Psr\Http\Message\ResponseInterface as Response;
  6. use App\Model\Car;
  7.  
  8. final class HomeController extends BaseController
  9. {
  10. public function example(Request $request, Response $response, $args)
  11. {
  12. $em = $this->em;
  13. $page = ($request->getParam('page', 0) > 0) ? $request->getParam('page') : 1;
  14. $limit = 3; // Number of posts on one page
  15. $offset = ($page - 1) * $limit;
  16. $count = count($em->getRepository('App\Model\Car')->findAll()); // Count of all available posts
  17.  
  18.  
  19. return $this->view->render($response, 'example.twig', [
  20. 'pagination' => [
  21. 'needed' => $count > $limit,
  22. 'count' => $count,
  23. 'page' => $page,
  24. 'lastpage' => (ceil($count / $limit) == 0 ? 1 : ceil($count / $limit)),
  25. 'limit' => $limit,
  26. ],
  27. // return list of Posts with Limit and Skip arguments
  28. 'posts' => $em->getRepository('App\Model\Car')->findBy([], null, $limit, $offset)
  29. ]);
  30. }
  31. }
  32.  
  33.  
  34. {% extends 'base.twig' %}
  35. {% block title %}Page exemple{% endblock %}
  36. {% block body %}
  37. <h1>Formulaire d'ajout</h1>
  38.  
  39. {% if pagination.needed %}
  40. <div class="ui pagination menu">
  41. {% for i in 1..pagination.lastpage %}
  42. <a class="{% if i == pagination.page %}active{% endif %} item" href="?page={{ i }}">{{ i }}</a>
  43. {% endfor %}
  44. </div>
  45. {% endif %}
  46.  
  47. <div class="ui container">
  48. {% for post in posts %}
  49. <a class="item">
  50. {# Post contents (title, url, ...) #}
  51. {{ dump(post.name) }}
  52. </a>
  53. {% endfor %}
  54. </div>
  55. {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement