覆盖KNP Paginator实体

时间:2016-09-02 12:54:08

标签: php symfony knppaginator knppaginatorbundle

我目前正在与KNP Paginator合作分页结果。 我得到序列化paginator对象并以下面的形式获得响应:

  "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" 

是否可以覆盖KNP分页器实体,以便响应不包括templates但包括total number of pages

期望的输出:

  "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

1 个答案:

答案 0 :(得分:0)

您可以采取两种方式:

1)定义KNP分页器的序列化,定义您想要显示的内容here(您还可以使用jms订阅者或banzigahatoas包定义prev / next路由)。控制器中的结果将是:

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

2)或以这种方式手工管理结果:

$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)
        ];

paginatorToArray方法类似于this

希望这个帮助