Good evening everybody,
I'm actually pushing my symfony2 website online and I have some issues. I have set the knp paginator bundle and have followed their intructions regarding the installation. But the fact is that my pagination is not working and therefore it only shows the items that are appearing on page one. When I click next/page2, the url seems to be behaving correctls but the page is still "freezed" the first display.
Here is my code (and I have all the config set up for the different bundles in the autoloader and the kernel):
// Controller/HomeController.php (used for the homepage)
<?php
namespace Pf\Bundle\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomeController extends Controller
{
public function indexAction()
{
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT a FROM PfBlogBundle:Projects a ORDER by a.id DESC";
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', 1)/*page number*/,
8/*limit per page*/
);
return $this->render('PfBlogBundle:Default:home.html.twig', array('pagination'=>$pagination));
}
}
and the twig
// Default/home.html.twig
{% extends '::home.html.twig'%}
{% block body %}
<div id="works">
<div class="container">
<div class="row-fluid">
<div class="span12">
{% for project in pagination %}
<div class="span3">
<div class="workBlocks">
<img src="{{ project.thumbnail }}" />
<div class="workCaption">
<h3>{{ project.title }}</h3>
<a href="{{ path('pf_blog_project', {'id': project.id}) }}">See</a>
</div><!-- end workCaption -->
</div><!-- end workBlocks -->
</div><!-- end span3 -->
{% endfor %}
</div>
{# display navigation #}
{{ knp_pagination_render(pagination) }}
</div><!-- end row -->
</div>
</div><!-- end works -->
{% endblock %}
I can't see where I'm wrong. All the help is much appreciated ;)
EDIT:
// this is the route I want to use...
pf_blog_index:
path: /
defaults: { _controller: PfBlogBundle:Home:index }
(EDIT) Well here I will continue by providing more details as I can't figure out what's wrong:
So in my vendors I have the following folder structure:
knplabs:
After having checked my autoload and my AppKernel, I have what I need to have regarding the doc..
In my composer.json :
"require": {
"knplabs/knp-components": "1.2.*@dev",
"knplabs/knp-menu-bundle": "2.0.*@dev",
"knplabs/knp-paginator-bundle": "dev-master"
},
In app/autoload.php I have
$loader->registerNamespaces(array(
'Knp\\Component' => __DIR__.'/../vendor/knp-components/src',
'Knp\\Bundle' => __DIR__.'/../vendor/bundles',
));
And finally, in my AppKernel.php
$bundles = array(
new Knp\Bundle\MenuBundle\KnpMenuBundle(),
new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
);
EDIT:
Also here are my settingy in the app/config.yml
knp_paginator:
page_range: 3 # default page range used in pagination control
default_options:
page_name: page # page query parameter name
sort_field_name: sort # sort field query parameter name
sort_direction_name: direction # sort direction query parameter name
distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements
template:
pagination: KnpPaginatorBundle:Pagination:twitter_bootstrap_pagination.html.twig # sliding pagination controls template
sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
Ok so I found THE or ONE solution to my problem. In fact as I said in my previous comments, the issue seemed to come from the routing. Knp bundle did effectively not appreciate the index route. Therefore, I have modified my controller in order to make a redirection with my indexAction(). Here is what works for me:
// Controller\HomeController.php
<?php
namespace Pf\Bundle\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class HomeController extends Controller
{
public function indexAction()
{
return $this->redirect($this->generateUrl('pf_blog_index2'));
}
public function listAction()
{
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT a FROM PfBlogBundle:Projects a ORDER by a.id DESC";
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', 1)/*page number*/,
8 /*limit per page */
);
// Puis modifiez la ligne du render comme ceci, pour prendre en compte l'article :
return $this->render('PfBlogBundle:Default:home.html.twig', array('pagination'=>$pagination));
}
}
// src/..../.../.../config/routing.php
pf_blog_index:
path: /
defaults: { _controller: PfBlogBundle:Home:index }
pf_blog_index2:
path: /home
defaults: { _controller: PfBlogBundle:Home:list }
I don't know if that's the best way to solve this issue but it works and in my case, I don't mind having such a url for the time being.
I hope that would help a beginner like me!