I have on the main page in the controller sitecontroller action
public function actionIndex() { $searchModel = new CourseSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } Coursesearch
public function search($params) { $query = Course::find()->active()->with(['category'])->orderBy(['id' => SORT_DESC]); // add conditions that should always apply here $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 2, ], ]); ............. Which displays a list of posts through the view.
<?= ListView::widget([ 'dataProvider' => $dataProvider, 'layout' => "{items}\n{pager}", 'itemView' => '_item', 'emptyText' => 'Ничего не найдено.', 'emptyTextOptions' => [ 'tag' => 'div class="alert alert-danger"' ], 'pager' => [ 'firstPageLabel' => '««', 'lastPageLabel' => '»»', 'nextPageLabel' => false, 'prevPageLabel' => false, 'maxButtonCount' => 5, ], ]); ?> And paginated navigation when you go for example to the second page gives the following form:
mysite.ru/?page=2&per-page=2 Here's how to make the page displayed mysite.ru/page-2
'urlManager' => [ 'class' => 'yii\web\UrlManager', // Disable index.php 'showScriptName' => false, // Disable r= routes 'enablePrettyUrl' => true, 'rules' => [ 'page-<page:\d+>-<per-page:\d+>' => 'site/index', '' => 'site/index', '<action:login|logout|signup|contact|about|request-password-reset>' => 'site/<action>', '<slug:[\w_\-]+>' => 'site/category', '<category:[\w]+>/<slug:[\w_\-]+>' => 'site/view',