21

I am developing an application using AngularJS for the frontend and Yii2 for the backend.

The frontend requires a comprehensive array of all users.

In the documentation of Yii2, http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html I can read the result divided by X-Pagination-Per-Page: 20

How do I set X-Pagination-Per-Page: ALL ??

Janka
  • 1,908
  • 5
  • 20
  • 41
  • This is an old question, but in my opinion, you should not have the Back end return all the users in one query which could become very big with time. Instead, your Front end should iterate all pages (using the Link header) until it reaches the end an concatenate all results. – Benoît Bouré May 14 '17 at 16:34

6 Answers6

17

If you are using yii\rest\ActiveController as parent from your controller, you need to override the action index in your controller.

This is my code

class StnkController extends ActiveController{
    public $modelClass = "common\models\Stnk";

    public function actions(){
        $actions = parent::actions();
        unset($actions['index']);
        return $actions;
    }

    public function actionIndex(){
        $activeData = new ActiveDataProvider([
            'query' => Stnk::find(),
            'pagination' => [
                'defaultPageSize' => 2,
            ],
        ]);
        return $activeData;
    }
}
Wilianto Indrawan
  • 2,394
  • 3
  • 21
  • 26
15

See this example from official documentation.

You can set any necessary number by changing pageSize parameter of pagination:

return new ActiveDataProvider([
    'pagination' => [
        'pageSize' => 10,
    ],
]);

Or you can disable pagination completely and show all available data like this:

return new ActiveDataProvider([
    'pagination' => false,
]);

I don't think this is a right way to pass ALL because it's taken directly from pageSize (which is number).

In case of disabling this header will not be added.

arogachev
  • 33,150
  • 7
  • 114
  • 117
9

Here says that you can get all records if you set pageSize less than 1.

But the pageSize must be in range of pageSizeLimit. pageSizeLimit has a default value [1, 50] so it can't be less than 1 unless you override it.

return new ActiveDataProvider([
    'pagination' => [
        'pageSizeLimit' => [0, 50],
    ],
]);

Now you can set pageSize value to 0 to get all records.

Example;

http://localhost/users?per-page=0

0

code for the data items on index page per page in yii2 framework

insert code into the models\ModelnameSearch.php File into the $query statement

public $pageSize=10;

$dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' =>  ['pageSize' => $pageSize,],       
    ]);
0

Almost all the answers suggest to customize the pagination by creating a new ActiveDataProvider. By doing so you lost lost the default behaviours of the yii\rest\IndexAction, such as sorting and filtering.

In fact, yii\rest\IndexAction is pretty flexible and allows overriding the minimal you need. To customize only the pagination, you can set the pagination property of the IndexAction without having to create your own ActiveDataProvider, e.g.:

class CatController extends \yii\rest\ActiveController
{
    public static $modelClass = '\app\models\Cat';

    public function actions()
    {
        return array_merge_recursive(parent::actions(), [
            'index' => [
                'pagination' => [
                    'defaultPageSize' => 50,
                    'pageSizeLimit' => [1, 50],
                ],
            ],
        ]);
    }
}

The trick above is the use of array_merge_recursive() such that the custom params are merged with the default ones, leaving the other default behaviours to work.

The entire list of pagination parameters can be found in the API doc. It is up to you to decide the combination that fits the application most, e.g.:

  • set a larger defaultPageSize
  • set pageSizeLimit to allow pageSize of 0, i.e. all items
  • disable pagination at all by setting it to false
Lacek
  • 1,595
  • 2
  • 11
  • 30
-1

In yii/rest/IndexAction.php change

return new ActiveDataProvider([
        'query' => $modelClass::find(),
    ]);

to

return new ActiveDataProvider([
        'query' => $modelClass::find(),
        'pagination' => false,
    ]);

That's all.

  • 1
    You should definitely not change the `IndexAction.php` within the framework. Imagine you were to update your dependencies, you would have to redo the change. Either use on of the other answers to do so, or create your own action as subclass from the class above and do your changes there. – Daniel Jan 19 '19 at 09:45