这些结果
{"id":19,"iso":"BB","name":"BARBADOS","nicename":"Barbados","iso3":"BRB","numcode":52,"phonecode":1246},{"id":20,"iso":"BY","name":"BELARUS","nicename":"Belarus","iso3":"BLR","numcode":112,"phonecode":375}]
,"_links":{"self":{"href":"http://localhost/travia-api/backend/web/v1/flight/index?id=4FR996IN2F829Md&page=1"},"next":{"href":"http://localhost/travia-api/backend/web/v1/flight/index?id=4FR996IN2F829Md&page=2"},"last":{"href":"http://localhost/travia-api/backend/web/v1/flight/index?id=4FR996IN2F829Md&page=12"}},"_meta":
{" TOTALCOUNT" 239" PAGECOUNT":12,"当前页":1," perPage":20 }}
如何更改perPage或禁用分页???
已更新!
设置标题内容类型但没有任何影响!
X-Pagination-Per-Page: 90
-- response --
200 OK
Date: Tue, 01 Sep 2015 12:29:45 GMT
Server: Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.19
X-Powered-By: PHP/5.5.19
X-Pagination-Total-Count: 239
X-Pagination-Page-Count:12 X-Pagination-Current-Page:1 X-Pagination-Per-Page:20
Content-Length: 2421
Keep-Alive:超时= 5,最大= 100 连接:保持活力 Content-Type:application / json;字符集= UTF-8
{"结果":[{" id":1," iso":" AF"," name&# 34;:" AFGHANISTAN"" nicename":"阿富汗"" ISO3":" AFG"&# 34; numcode&#34:4," phonecode" 93}, ... {" ID":20," ISO":" BY""名称":"白俄罗斯"&# 34; nicename":"白俄罗斯"," iso3":" BLR"," numco de":112,"电话代码":375}],_ meta":{" totalCount":239," pageCount":12," currentPage":1,&# 34; perPage" 20}}
答案 0 :(得分:2)
代码如下:
$query = SampleModel::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'defaultPageSize' => 2, //set page size here
]
]);
$relationShips = $dataProvider->getModels();
答案 1 :(得分:2)
我知道这个问题很旧,但是在整个Internet上,我都看到很多关于在使用REST API每页参数实现此问题时实现此问题的坏建议。这是一个两部分的答案。
首先,您必须在url中而不是在标题中指定页面参数。查看使用CRUD生成器时gridview如何更改URL。
例如。这是一个网址,每个响应将返回10个项目
http://www.example.com?per-page=10
此网址将返回50
http://www.example.com?per-page=50
这就是陷阱。默认分页数最多为50。请参见:https://www.yiiframework.com/doc/api/2.0/yii-data-pagination# $ pageSizeLimit-detail。如果您想要50条以上的记录,则必须在分页中更改此参数,如下所示:
$query = SampleModel::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
pageSizeLimit = [1, 1000]
]
]);
现在您可以在每页1-1000条记录中指定任何地方。
X-Pagination-Current-Page→1
X-Pagination-Page-Count→2
每页X-Pagination→100
X分页总数→157
要选择一个页面,您仍然必须在URL中指定它。这是157条记录中的第2页,每页30条记录。
http://www.example.com?page=2&per-page=30
以下是响应标头:
X-Pagination-Current-Page→2
X-Pagination-Page-Count→6
每页X-Pagination→30
X分页总数→157
最后,如果您设置为返回没有人应该做的所有记录,则可以将pageSizeLimit下限设置为零,如下所示:
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
pageSizeLimit = [0, 1000]
]
]);
然后将每页指定为0。
http://www.example.com?per-page=0
这将返回所有记录。
这是所有记录的标题响应
X-Pagination-Current-Page→1
X-Pagination-Page-Count→1
每页X-Pagination→0
X分页总数→157
同样,没有人可以这样做。您应该循环浏览分页以构建阵列,但这真是疯狂的话题。如果您有超过一百万条记录怎么办?无论如何,这是您的项目。对于2018年任何关注此事的人来说,祝好运,因为文档中并未明确说明。
答案 2 :(得分:1)
您可以通过修改HTTP标题X-Pagination-Per-Page
检查文档以获取更多信息here
UPDATE1: 对不起,我匆匆忙忙地发布错误答案。以下是如何更改页面大小的方法。你需要查看ActiveDataProvider - 它有 $ pagination 属性(你可以阅读它here)。将$pageSize或$defaultPageSize设置为您需要的值应该有效。
答案 3 :(得分:1)
有关数据序列化,请参阅yii2文档 它将在响应中为您分页数据
https://www.yiiframework.com/doc/guide/2.0/en/output-pagination
您也可以在控制器类中使用此代码段
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
public $serializer = [
'class' => 'yii\rest\Serializer',
'collectionEnvelope' => 'items',
];
}
这将是响应格式
HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
X-Powered-By: PHP/5.4.20
X-Pagination-Total-Count: 1000
X-Pagination-Page-Count: 50
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://localhost/users?page=1>; rel=self,
<http://localhost/users?page=2>; rel=next,
<http://localhost/users?page=50>; rel=last
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
{
"items": [
{
"id": 1,
...
},
{
"id": 2,
...
},
...
],
"_links": {
"self": {
"href": "http://localhost/users?page=1"
},
"next": {
"href": "http://localhost/users?page=2"
},
"last": {
"href": "http://localhost/users?page=50"
}
},
"_meta": {
"totalCount": 1000,
"pageCount": 50,
"currentPage": 1,
"perPage": 20
}
}