Kunwar Siddharth Singh Kunwar Siddharth Singh - 4 months ago 28
PHP Question

How to implement rel=prev and rel=next to Symfony2.3 pagination?

I am using Symfony2.3 for my project and for pagination I am using KNP paginator Bundle.
I want to know that How I can implement rel=prev and rel=next in our page ?

My Controller :

<?php

namespace XXX\ABCBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Session\Session;
use Doctrine\ORM\EntityManager;

/**
* author Siddharth Singh (siddharth.find@gmail.com)
*/
class ArcController extends Controller {

/**
* @Route("/arch", name="_arch")
* @Template()
*/
public function indexAction() {
$em = $this->getDoctrine()->getEntityManager();
$AArtEntity = $em->getRepository('XXXABCBundle:Arc')->findAll(array('updated'=>'DESC'));
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$AArtEntity, $this->get('request')->query->get('page', 1)/* page number */, 10/* limit per page */

);

return array('article' => $pagination);
}

}


Twig File :-

{% extends 'XXXABCBundle::layout.html.twig' %}
{% block body %}
{% for artic in article %}
<div class="txt">
<p>{{artic.description|striptags|titletruncate(250)}}</p>
</div>
{% endfor %}
<div class="arcive_Paging">
<ul class="ul_paging">
<span class="acitve">{{ knp_pagination_render(article) }}</span>
</ul>
</div>
{% endblock %}


Thanks !

Answer

You can do it by overriding default pagination template. Check Templates part of the official documentation:

https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/Resources/doc/templates.md

for example:

Tell KnpPaginatorBundle to load your template for rendering pagination, add these lines of code to config.yml:

knp_paginator:
    template:
        pagination: YourBundle::pagination.html.twig

And copy default pagination template code to your template in src/YourBundle/Resources/views/pagination.html.twig. You can get the code here:

https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/Resources/views/Pagination/sliding.html.twig

Comments