Yii2 restful pagination support

In most cases, our data needs to be paginated. However, Yii2 restful
does not display paging information by default. We do not know which page is currently, how to access the next page, so how can we display the paging data? Don't worry, let's illustrate with an example.

1 Create a new stock interface, [refer to Yii2 Restful to quickly build CURD][1]

2 Access the information list and find that there is no paging data

操作 : stock/index
URL : GET http://api.baojia.local/v1/stocks

 
Screenshot of 2017-08-23 10-26-29.png

3 Modify the StockController and add the $serializer property

File path: api\modules\v1\controllers\StockController.php

<?php
namespace api\modules\v1\controllers; use yii\rest\ActiveController; class StockController extends ActiveController { public $modelClass = 'api\models\Stock'; public $serializer = [ 'class' => 'yii\rest\Serializer', 'collectionEnvelope' => 'items', ]; 

4 Visit the information list again and find that the data is under the items item, and there are paging information in _links and _metadata

 
Screenshot of 2017-08-23 10-24-20.png

3 Use the next item in _links to access the next page.

self represents the current page
next represents the next page
last represents the last page

 




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326546389&siteId=291194637