1

我目前正在使用 KNP 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
4

1 回答 1

0

您可以通过两种方式进行:

1) 定义 KNP 分页器的序列化,定义您想要显示的内容,如此所述(您还可以使用 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 方法是这样的。

希望这有帮助

于 2016-09-02T13:58:10.160 回答