Yii2: Setting Rest pageSize Variable in yii2?

Created on 8 Feb 2017  路  4Comments  路  Source: yiisoft/yii2

I Want to set number of items per page in rest app.
I tested every items that change this value (pageSize) in activeDataProvider instance of rest Controller Like Overriding prepareDataProvider method or actionIndex.
But It Dose not effect.
Every time run code I get the default 20 items per page that set in activeDataProvider
What is the solution?

| Q | A
| ---------------- | ---
| Yii version | 2.0.10
| PHP version | 2.5.6
| Operating system | Mac OS Sierra 10.12.2

question

Most helpful comment

    public function actions()
    {
        $actions = ArrayHelper::merge(
            parent::actions(),
            [
                'index' => [
                    'prepareDataProvider' => function ($action) {
                        /* @var $modelClass \yii\db\BaseActiveRecord */
                        $modelClass = $action->modelClass;

                        return Yii::createObject([
                            'class' => ActiveDataProvider::className(),
                            'query' => $modelClass::find(),
                            //'pagination' => false,
                            'pagination' => [
                                'pageSize' => 5,
                            ],
                        ]);
                    },
                ],
            ]
        );

        return $actions;
    }

All 4 comments

I believe the REST controller provides all the RESTFUL actions you need by default without the developer needing to write additional code. If you want to customize things like pagination size, you have to unset an action method to disable the inherited action and then provide your own method to handle data.

public function actions()
    {
        $actions = parent::actions();

        //disable "index", "create", "update", "delete", "options" actions
        unset(
            $actions['index'],
            $actions['create'],
            $actions['update'],
            $actions['delete'],
            $actions['options']
        );

        return $actions;

    }

Then redefine actionIndex() method yourself to do what you need.

    public function actionIndex()
    {
        //\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $myQuery = mySpecialModel::find();
        $dataProvider = new ActiveDataProvider([
            'query'      => $myQuery,
            'pagination' => [
                'pageSize' => 0,  // ALL results, no pagination
            ],
            'sort'       => [
                'defaultOrder' => [
                    'sortOrder' => SORT_ASC,
                    'manu'      => SORT_ASC,
                ],
            ],
        ]);

        return $dataProvider;
    }
    public function actions()
    {
        $actions = ArrayHelper::merge(
            parent::actions(),
            [
                'index' => [
                    'prepareDataProvider' => function ($action) {
                        /* @var $modelClass \yii\db\BaseActiveRecord */
                        $modelClass = $action->modelClass;

                        return Yii::createObject([
                            'class' => ActiveDataProvider::className(),
                            'query' => $modelClass::find(),
                            //'pagination' => false,
                            'pagination' => [
                                'pageSize' => 5,
                            ],
                        ]);
                    },
                ],
            ]
        );

        return $actions;
    }

@JamesLeon
Thanks James
Your solution Worked! :)
I thought actionIndex() was override

Thank you for your question.
In order for this issue tracker to be effective, it should only contain bug reports and feature requests.

We advise you to use our community driven resources:

If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.

_This is an automated comment, triggered by adding the label question._

Was this page helpful?
0 / 5 - 0 ratings