1. In Yii2, how to implement routing for case/. Configuring ‘case/’ => ‘use-case/index’ is invalid; opening /case/ results in a 302 redirect to /case. As shown in Figure 1.

2. Adjust the configuration as follows: ‘suffix’ => ‘/’. The response is 200, and opening /case automatically redirects to /case/ as expected. As shown in Figure 2.

PHP
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'normalizer' => [
'class' => yii\web\UrlNormalizer::class,
'collapseSlashes' => true,
'normalizeTrailingSlash' => true,
],
'rules' => [
// 精彩案例
[
'pattern' => 'case',
'route' => 'use-case/index',
'suffix' => '/',
],
],
],
3. The generated link is: Featured Cases. This meets expectations. As shown in Figure 3.
![<?= Html::a('Featured Cases', ['use-case/index'], ['target' => '_blank']) ?> generates the link: <a href="/case/" target="_blank">Featured Cases</a>. This meets expectations.](https://media.shuijingwanwq.com/2025/07/3-3.jpg)
4. /case/list-.html is the link when clicking the first page. The desired link is /case/
PHP
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 9,
'pageSizeParam' => false,
'pageParam' => 'page',
],
'sort' => [
'defaultOrder' => ['id' => SORT_DESC],
],
]);
$dataProvider->pagination,
'options' => ['class' => 'page'], //
'linkContainerOptions' => ['tag' => 'li'],
'activePageCssClass' => 'active',
'prevPageCssClass' => 'prev',
'nextPageCssClass' => 'next',
'disabledPageCssClass' => 'disabled',
'nextPageLabel' => '下一页',
'prevPageLabel' => '上一页',
]) ?>
5. Custom Pagination class. Yii starts pagination from page 0 by default, where the first page is page=0. You need to override the pagination class to handle the specific URL for the first page.
PHP
params === null ? Yii::$app->getRequest()->getQueryParams() : $this->params;
// 移除 page 参数
unset($params[$this->pageParam]);
if ($page > 0) {
// 页码从0开始,所以需要 +1
$params[$this->pageParam] = $page + 1;
// 第二页及以上:/case/list-2.html
return Yii::$app->getUrlManager()->createUrl(array_merge(
['use-case/index'],
$params
));
}
// 第1页:/case/
return Yii::$app->getUrlManager()->createUrl(array_merge(
['use-case/index'],
$params
));
}
}
PHP
// $dataProvider = new ActiveDataProvider([
// 'query' => $query,
// 'pagination' => [
// 'pageSize' => 9,
// 'pageSizeParam' => false,
// 'pageParam' => 'page',
// ],
// 'sort' => [
// 'defaultOrder' => ['id' => SORT_DESC],
// ],
// ]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => new CleanPagination([
'totalCount' => $query->count(),
'pageSize' => 9,
'pageSizeParam' => false,
'pageParam' => 'page',
]),
'sort' => [
'defaultOrder' => ['id' => SORT_DESC],
],
]);
