覆盖KNP分页器实体



我目前正在使用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)定义KNP分页器的序列化,定义您想要显示的内容,如此处所述(您还可以使用jms订阅者或banzigahatoas bundle定义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方法是这样的

希望对您有所帮助

相关内容

  • 没有找到相关文章

最新更新