How to implement the route of case/ in Yii2
1. In Yii2, how to implement the route of case/. If configuredcase/=>use-case/index, is invalid, open /case/ will redirect to /case. As shown in Figure 1
2. Adjust the configuration as follows,Suffix=>/ / *, the response is 200 , and /case is opened, and it will also automatically jump to /case/ in line with expectations. as shown in Figure 2
'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 links are:Highlights . in line with expectations. as shown in Figure 3
4. /case/list-.html The link when clicking on page 1 is like this. hope is /case/
$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. The custom Pagination class Yii is paging from page 0 by default, the first page is page=0, and the page class needs to be overwritten to process the separate URL of the first page.
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
));
}
}
// $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],
],
]);


![<?= Html::a('精彩案例', ['use-case/index'], ['target' => '_blank']) ?> 生成的链接为:<a href="/case/" target="_blank">精彩案例</a> 。符合预期。](https://www.shuijingwanwq.com/wp-content/uploads/2025/07/3-3.jpg)