没有不值得去解决的问题,也没有不值得去学习的技术!

How to Implement case/ Routing in Yii2

In Yii2, how to implement routing for case/. Configuring 'case/' => 'use-case/index' is invalid; opening /case/ results in a 302 redirect to /case.

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.

In Yii2, how to implement routing for case/. Configuring 'case/' => 'use-case/index' is invalid; opening /case/ results in a 302 redirect to /case.
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.

Adjust the configuration as follows: 'suffix' => '/'. The response is 200, and opening /case automatically redirects to /case/ as expected.
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.
Figure 3

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],
            ],
        ]);

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系