public function actions() { return array_merge(parent::actions(), ['index' => ['class' => 'yii\\rest\\IndexAction', 'modelClass' => $this->modelClass, 'checkAccess' => [$this, 'checkAccess'], 'prepareDataProvider' => function ($action) { $m = new CalculationSearch(); $params = \Yii::$app->getRequest()->getQueryParams(); $dataProvider = $m->search($params); return $dataProvider; }]]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = CalculationSearch::find(); $sort = new Sort(['attributes' => ['id', 'title']]); if (!empty($params['order'])) { $typeOrder = $params['order'][0] == '+' ? SORT_ASC : SORT_DESC; $sort->defaultOrder = array(substr($params['order'], 1, strlen($params['order'])) => $typeOrder); } $pages = new Pagination(); $perPage = isset($params['perPage']) ? $params['perPage'] : null; if (!empty($perPage)) { $pages->pageSize = $perPage; $pages->page = isset($params['page']) ? $params['page'] : null; if ($perPage == 'all') { $pages = false; } } $this->load(['CalculationSearch' => $params]); $query->andFilterWhere(['like', 'title', $this->title]); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => $sort, 'pagination' => $pages]); return $dataProvider; }