Permalink
Browse files

feature #154 Added a paginator using KnpPaginatorBundle (ccab)

This PR was squashed before being merged into the master branch (closes #154).

Discussion
----------

Added a paginator using KnpPaginatorBundle

Please review and let me know

Commits
-------

364bf08 Added a paginator using KnpPaginatorBundle
2 parents e77f434 + 364bf08 commit 2731baea495a8315138fa8ee8ff87ca8b8ad6fb4 @javiereguiluz javiereguiluz committed Sep 15, 2015
View
@@ -20,6 +20,7 @@ public function registerBundles()
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
+ new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
new AppBundle\AppBundle(),
);
@@ -16,6 +16,10 @@
{% else %}
<div class="well">{{ 'post.no_posts_found'|trans }}</div>
{% endfor %}
+
+ <div class="navigation">
+ {{ knp_pagination_render(posts) }}
+ </div>
{% endblock %}
{% block sidebar %}
View
@@ -68,7 +68,7 @@
echo ' than the one used with your web server. To be on the'.PHP_EOL;
echo ' safe side, please check the requirements from your web'.PHP_EOL;
echo ' server using the ';
-echo_style('yellow', 'web/config.php');
+echo_style('yellow', 'web/check.php');
echo ' script.'.PHP_EOL;
echo PHP_EOL;
@@ -94,3 +94,8 @@ swiftmailer:
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
+
+# KnpPaginatorBundle configuration (used to paginate large result set)
+knp_paginator:
+ template:
+ pagination: KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig # sliding pagination controls template
View
Binary file not shown.
View
@@ -16,6 +16,7 @@
"ezyang/htmlpurifier" : "~4.7",
"incenteev/composer-parameter-handler" : "~2.1",
"ircmaxell/password-compat" : "~1.0",
+ "knplabs/knp-paginator-bundle" : "~2.4",
"leafo/scssphp" : "~0.1.5",
"patchwork/jsqueeze" : "~1.0",
"sensio/distribution-bundle" : "~3.0.28",
View
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
- "hash": "04755c35eaa37511db107f6bb4f50ca0",
+ "hash": "06081ad54e57c156efffe99d6e7ee2e9",
"packages": [
{
"name": "doctrine/annotations",
@@ -1051,6 +1051,135 @@
"time": "2014-01-12 16:20:24"
},
{
+ "name": "knplabs/knp-components",
+ "version": "1.3.1",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/KnpLabs/knp-components.git",
+ "reference": "6d091c2f16eec2769db38f38e08c7bd5847ce072"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/KnpLabs/knp-components/zipball/6d091c2f16eec2769db38f38e08c7bd5847ce072",
+ "reference": "6d091c2f16eec2769db38f38e08c7bd5847ce072",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.2"
+ },
+ "require-dev": {
+ "doctrine/mongodb-odm": "~1.0@beta",
+ "doctrine/orm": "~2.4",
+ "phpunit/phpunit": "~4.2",
+ "ruflin/elastica": "~1.0",
+ "symfony/event-dispatcher": "~2.5"
+ },
+ "suggest": {
+ "doctrine/common": "to allow usage pagination with Doctrine ArrayCollection",
+ "doctrine/mongodb-odm": "to allow usage pagination with Doctrine ODM MongoDB",
+ "doctrine/orm": "to allow usage pagination with Doctrine ORM",
+ "propel/propel1": "to allow usage pagination with Propel ORM",
+ "ruflin/Elastica": "to allow usage pagination with ElasticSearch Client",
+ "solarium/solarium": "to allow usage pagination with Solarium Client"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.2.x-dev"
+ }
+ },
+ "autoload": {
+ "psr-0": {
+ "Knp\\Component": "src"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "KnpLabs Team",
+ "homepage": "http://knplabs.com"
+ },
+ {
+ "name": "Symfony Community",
+ "homepage": "http://github.com/KnpLabs/knp-components/contributors"
+ }
+ ],
+ "description": "Knplabs component library",
+ "homepage": "http://github.com/KnpLabs/knp-components",
+ "keywords": [
+ "components",
+ "knp",
+ "knplabs",
+ "pager",
+ "paginator"
+ ],
+ "time": "2014-10-06 10:38:10"
+ },
+ {
+ "name": "knplabs/knp-paginator-bundle",
+ "version": "2.4.2",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/KnpLabs/KnpPaginatorBundle.git",
+ "reference": "800eb12f225573869b6f04d77a6e329a6da9110f"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/KnpLabs/KnpPaginatorBundle/zipball/800eb12f225573869b6f04d77a6e329a6da9110f",
+ "reference": "800eb12f225573869b6f04d77a6e329a6da9110f",
+ "shasum": ""
+ },
+ "require": {
+ "knplabs/knp-components": "~1.2",
+ "php": ">=5.3.3",
+ "symfony/framework-bundle": "~2.0",
+ "twig/twig": "~1.5"
+ },
+ "require-dev": {
+ "symfony/expression-language": "~2.4"
+ },
+ "type": "symfony-bundle",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "2.4.x-dev"
+ }
+ },
+ "autoload": {
+ "psr-4": {
+ "Knp\\Bundle\\PaginatorBundle\\": ""
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "KnpLabs Team",
+ "homepage": "http://knplabs.com"
+ },
+ {
+ "name": "Symfony2 Community",
+ "homepage": "http://github.com/KnpLabs/KnpPaginatorBundle/contributors"
+ }
+ ],
+ "description": "Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features",
+ "homepage": "http://github.com/KnpLabs/KnpPaginatorBundle",
+ "keywords": [
+ "Symfony2",
+ "bundle",
+ "knp",
+ "knplabs",
+ "pager",
+ "pagination",
+ "paginator"
+ ],
+ "time": "2015-05-20 15:09:02"
+ },
+ {
"name": "kriswallsmith/assetic",
"version": "v1.3.0",
"source": {
@@ -34,12 +34,20 @@
class BlogController extends Controller
{
/**
- * @Route("/", name="blog_index")
+ * @Route("/", name="blog_index", defaults={"page" = 1})
+ * @Route("/page/{page}", name="blog_index_paginated", requirements={"page" : "\d+"})
*/
- public function indexAction()
+ public function indexAction($page)
{
- $entityManager = $this->getDoctrine()->getManager();
- $posts = $entityManager->getRepository('AppBundle:Post')->findLatest();
+ $query = $this->getDoctrine()->getRepository('AppBundle:Post')->queryLatest();
+
+ $paginator = $this->get('knp_paginator');
+ $posts = $paginator->paginate(
+ $query,
+ $page,
+ Post::NUM_ITEMS
+ );
+ $posts->setUsedRoute('blog_index_paginated');
return $this->render('blog/index.html.twig', array('posts' => $posts));
}
@@ -68,7 +68,7 @@ private function loadUsers(ObjectManager $manager)
private function loadPosts(ObjectManager $manager)
{
- foreach (range(1, 10) as $i) {
+ foreach (range(1, 30) as $i) {
$post = new Post();
$post->setTitle($this->getRandomPostTitle());
@@ -24,12 +24,7 @@
*/
class PostRepository extends EntityRepository
{
- /**
- * @param int $limit
- *
- * @return Post[]
- */
- public function findLatest($limit = Post::NUM_ITEMS)
+ public function queryLatest()
{
return $this->getEntityManager()
->createQuery('
@@ -39,8 +34,11 @@ public function findLatest($limit = Post::NUM_ITEMS)
ORDER BY p.publishedAt DESC
')
->setParameter('now', new \DateTime())
- ->setMaxResults($limit)
- ->getResult()
;
}
+
+ public function findLatest()
+ {
+ $this->queryLatest()->getResult();
+ }
}

0 comments on commit 2731bae

Please sign in to comment.