0
votes

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:

  • knp-components
  • knp-menu
  • knp-menu-bundle
  • knp-paginator-bundle

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
4
Can you please paste your routing? - Kapil
the code is okay, the only reason I can imagine is that you call the controller in a subrequest(using render in twig) and therefore does not have a parameter in the request. try to dump request parameters with var_dump($this->get('request')->query->all()); - Marino Di Clemente
I'm not sure what you mean..? sorry :/ - daneczech
Actually the var dump returns : array(0) { } I can see the point..This means it doesn't find the objects to sort right? That might be due to my custom query right? Sorry if those questions sounds stupid but it's not always easy to learn all of this and I would make sure I understand. Thank you - daneczech
Well, even if I do a more "generic" request the result stay the same.. nothing happens on my page :/ $repository = $this->getDoctrine() ->getRepository('PfBlogBundle:Projects'); $query = $repository->findAll(); - daneczech

4 Answers

1
votes

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!

0
votes

Pay attention on this code

$this->get('request')->query->get('page', 1)/*page number*/

You get the 'page' value of _GET parameter of request,by this code , but you have to get your 'page' as Request Attribute, not as value of _GET. In other words the right code is

$this->get('request')->get('page')
0
votes

The example in KnpPaginatorBundle repo shows that you need to pass Request $request as a parameter to the controller method that creates $pagination, which is indexAction in your case. I noticed that you don't have this parameter. This is probably why the paginator is not paginating. I've had exactly the same problem and solved it by including the $request parameter in the controller, as the example in KnpPaginatorBundle repo shows. Also, you need to remember to include use Symfony\Component\HttpFoundation\Request in the controller class.

0
votes

I have the same problem, after many hour of search, I finaly find the solution, we just have to put

$request = Request::createFromGlobals();

before and after get the variable you need, in my case a get it from "GET" by doing

$request->query->get('variable')