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', 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Open config/web.php and add to components:

 'components' => [ 'urlManager' => [ 'class' => 'yii\web\UrlManager', // Disable index.php 'showScriptName' => false, // Disable r= routes 'enablePrettyUrl' => true, 'rules' => [ '<controller:\w+>/<action:\w+>/page-<page:\d+>' => '<controller>/<action>', '<controller:\w+>/<action:\w+>' => '<controller>/<action>', ], //Далее какой-то код. ], 

The code is working, if there are any problems, lay out the config/web.php and we will help.

  • '<controller:\w+>/<action:\w+>/page-<page:\d+>/per-page-<per-page:\d+>' => '<controller>/<action>', You can add the first line, what would? per-page = 2 also formed in the CNC - ilyaplot
  • 2
    Or maybe he does not want? Or do you want to do per_page-hello-world: <per-page: \ d +>? All cases describe? I answered on the case. - Urmuz Tagizade
  • This is how my config came out, and now everything works, I have everything displayed on the main page, posts and categories - Vasya Smith
  • Code added in question - Vasya Smith
  • So mark the answer with a solution, since everything works. - ilyaplot