R
R
ryzhak2014-11-20 01:39:42
Yii
ryzhak, 2014-11-20 01:39:42

How to select all values ​​at once in yii2 restful in server response without pagination?

In general, I am currently making a mobile application. Under it, a backend was made on yii2 rest api.
The response from the server to the /games request comes with approximately the following values:

[{"championship":"Indian I-League","homeTeam":"Mumbai City","awayTeam":"SC Goa"},
{"championship":"Gulf Cup of Nations","homeTeam":"UAE","awayTeam":"Kuwait"}
...]

Response headers:
...
Server →Apache/2.4.7 (Ubuntu)
X-Pagination-Current-Page →1
X-Pagination-Page-Count →7
X-Pagination-Per-Page →20
X-Pagination-Total-Count →121
X-Powered-By →PHP/5.5.9-1ubuntu4.5

So, in the server response, only 20 values ​​come in for one request, and the total values, as can be seen from X-Pagination-Total-Count, are 121. Therefore, to select all the values, you need to make 6 more requests, like:
/games?page=2
/ games?page=3
/games?page=4 etc.
Logically, when a user enters a mobile application, it loads data from a server. So, when loading, I need to do all 7 requests, or can I do it somehow?
I hope the question is clear
Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
iplus, 2014-11-22
@ryzhak

You can set the page size:
When /games is requested, yii\rest\IndexAction is executed , which returns ActiveDataProvider(['query' => $modelClass::find(),]) . Those. the Pagination object is default there ($pageParam = 'page', $pageSizeParam = 'per-page', $pageSizeLimit = [1, 50]). And the default yii\data\Pagination tries to take parameters from $request->getQueryParams() .

S
Sergey, 2014-11-20
Protko @Fesor

Just explain to the API developers that for REST it's better to do this:
/games?start_from=2&limit=20 + header give all X-Pagination-* headers ... well, except perhaps Page-Count and Current-Page
Thus, if you have to change on the client the number of items per page, you don’t have to kick the API developers.
More on your problem. 7 requests is stupid. If you need to make a mechanism for caching data, for example, then yes, then we take all the data (although again, you can do this gradually, it all depends on the goals you are following) and store them periodically by invalidating the cache.
In general, it can be one request that receives the first portion of data and loads a new portion as needed. For example, if you have pagination implemented and you want everything to work as quickly as possible, you can, when scrolling the screen, if the user approaches pagination, immediately make a request for the next batch of data so that while the user clicks on the next button, the data would already be loaded. Well, or do an endless scroll. As already mentioned, it all depends on your use case. But still pass on recommendations regarding the API, because there will be problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question