1

I am currently working with KNP Paginator to paginate results. I get serialize paginator object and get response in following form:

  "current_page_number": 1
  "num_items_per_page": 5
  "items": [5]
  "total_count": 35
  "paginator_options": {
  "pageParameterName": "page"
  "sortFieldParameterName": "sort"
  "sortDirectionParameterName": "direction"
  "filterFieldParameterName": "filterField"
  "filterValueParameterName": "filterValue"
  "distinct": true
  }-
  "custom_parameters": [0]
  "route": "api_tags_list"
  "params": [0]
  "page_range": 5
  "template": "KnpPaginatorBundle:Pagination:sliding.html.twig"
  "sortable_template": 
  "KnpPaginatorBundle:Pagination:sortable_link.html.twig"
  "filtration_template": 
  "KnpPaginatorBundle:Pagination:filtration.html.twig" 

Is it possible to override KNP paginator entity so that response will not include templates but included total number of pages?

Desired output:

  "current_page_number": 1
  "num_items_per_page": 5
  "items": [5]
  "total_count": 35
  "paginator_options": {
  "pageParameterName": "page"
  "sortFieldParameterName": "sort"
  "sortDirectionParameterName": "direction"
  "filterFieldParameterName": "filterField"
  "filterValueParameterName": "filterValue"
  "distinct": true
  }-
  "custom_parameters": [0]
  "route": "api_tags_list"
  "params": [0]
  "page_range": 5
  "total_page_number": 7
blahblah
  • 1,010
  • 15
  • 40

1 Answers1

0

You could proceed in two way:

1) Define the serialization of the KNP paginator defining what you want to display as described here (you could also define the prev/next route with a jms subscriber or with banzigahatoas bundle). The result in the controller will be:

 $paginator  = $this->get('knp_paginator');
        $data =
            $paginator->paginate(
                $query, /* query NOT result */
                2 /*page number*/,
                2/*limit per page*/
            );
         return $data;

2) Or managed by hand the result in this manner:

$paginator  = $this->get('knp_paginator');
        $data =
            $paginator->paginate(
                $query, /* query NOT result */
                2 /*page number*/,
                2/*limit per page*/
            );
        return [
            'items' => $data->getItems(),
            'pagination' => $this->paginationToArray($data)
        ];

Where paginatorToArray method is something like this.

Hope this help

Community
  • 1
  • 1
Matteo
  • 37,680
  • 11
  • 100
  • 115
  • first option looks like really good solution, bit it doesn't work for me. I cleared cache, but still don't get expected result. second option doesn't suit my current controller response format. Thanks anyway – blahblah Sep 02 '16 at 15:06
  • Hi @blahblah very stange, I'm sorry... I used both in my projects.. let me know how you solved. Good luck – Matteo Sep 03 '16 at 06:31