Yii2 分页不改变页面

Yii2 pagination doesn't change page

我的寻呼机没有切换到所选页面。当我单击下一页按钮时,它会重新加载页面。我也试过没有 Pjax 容器,但结果是一样的。我红了其他帖子,但仍然无法弄清楚。我正在做的是:

$categoriesProvider = new ActiveDataProvider([
            'query' => Page::find()->where(['enable' => 1, 'id_in' => $page_id])->orderBy('sort ASC'),
            'pagination' => [
                'pageSize' => 1,
                'route' => "/".Yii::$app->getRequest()->getQueryParam('page')
            ]
        ]);

        return $this->render('index', [
            'categoriesProvider' => $categoriesProvider
        ]);

并且在视图中:

<?php Pjax::begin() ?>

    <div class="content-area col-md-8 col-sm-6 col-xs-12 no-padding">

        <?php if(!empty($categoriesProvider)){
            echo ListView::widget([
                'dataProvider' => $categoriesProvider,
                'layout' => "{summary}\n{items}
                                \n<nav class='post-pagination col-md-12 col-sm-12 col-xs-12'>
                                    {pager}
                                </nav>",
                'itemView' => function($model, $key, $index, $widget){
                    return $this->render('_category', [
                        'model' => $model,
                    ]);
                },
                'pager' => [
                    'nextPageLabel' => ">>",
                    'prevPageLabel' => "<<",
                    'maxButtonCount' => 5,
                    'options' => [
                        'tag' => 'ul',
                        'class' => 'pagination',
                    ]
                ]
            ]);
        }?>
    </div><!-- Content Area /- -->

    <?php Pjax::end(); ?>

URL配置:

<?php

namespace frontend\controllers;

use backend\models\News;
use backend\models\Page;
use backend\models\Product;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

class SplitterController extends Controller
{
    public function actionManageRequest()
    {
        $param_page = \Yii::$app->getRequest()->getQueryParam('page');
        $param_category = \Yii::$app->getRequest()->getQueryParam('category');
        $param_product = \Yii::$app->getRequest()->getQueryParam('product');

        $page = $this->getModel($param_page, new Page());

        //If page is existing page model go forward
        if(!empty($page)){

            $controller = $this->getController($page->view);

            if($this->checkId($param_page) === 'news'){
                $model = new News();
            }else{
                $model = new Page();
            }

            $category = $this->getModel($param_category, $model);

            if(!empty($category)){

                $product = $this->getModel($param_product, new Product());

                //If product is existing product model - go forward to single view
                if(!empty($product)){

                    $this->registerTags($product);

                    try{
                        return \Yii::$app->runAction("$controller/view");
                    }
                    catch (\Throwable $e){
                        throw new NotFoundHttpException('Page not found',404);
                    }
                }

                //If product is not empty and product don't - throw exception
                if(!empty($param_product) && $product === null){
                    throw new NotFoundHttpException('Page out found', 404);
                }

                $this->registerTags($category);

                //If page model is news page - render news single view
                if($this->checkId($param_page) === 'news'){
                    return \Yii::$app->runAction("$controller/multi-view");
                }

                try{
                    return \Yii::$app->runAction("$controller/multi-view");
                }
                catch (\Throwable $e){
                    throw new NotFoundHttpException('Page not found',404);
                }
            }

            $this->registerTags($page);

            //If category is not empty but no such page found - throw exception
            if(!empty($param_category) && $category ===  null){
                throw new NotFoundHttpException('Page not found', 404);
            }

            return \Yii::$app->runAction($page->view);
        }

        //If page is not empty but no such page found - throw exception
        if(!empty($param_page) && $page ===  null){
            throw new NotFoundHttpException('Page not found', 404);
        }

        $page = Page::findOne(13);
        $this->registerTags($page);

        return \Yii::$app->runAction($page->view);
    }

    private function getModel($param, $model)
    {
        $chunks = explode('-', $param);
        $chunk_id = end($chunks);
        return $model::findOne($chunk_id);
    }

    private function registerMetaTags($name, $content)
    {
        \Yii::$app->view->registerMetaTag([
            'name' => $name,
            'content' => $content
        ]);
    }

    private function registerTitle($title)
    {
        \Yii::$app->view->title = $title;
    }

    private function checkId($param)
    {
        $id = explode('-', $param);
        $id = end($id);

        switch ($id) {
            case 14:
                return 'news';
                break;
            default:
                return 'page';
                break;
        }
    }

    private function getController($path)
    {
        $controller = explode('/', $path);
        return $controller[0];
    }

    private function registerTags($model)
    {
        $this->registerMetaTags('description', $model->meta_title);
        $this->registerTitle($model->meta_title);
    }
}

Url 经理规则:

'rules' => [
                '<page>/<category>/<product>' => 'splitter/manage-request',
                '<page>/<category>' => 'splitter/manage-request',
                '<page>' => 'splitter/manage-request',
                '' => 'splitter/manage-request'
            ],

看起来 Pagination 参数与您的 URL 规则冲突。 Pagination 使用 page GET 参数将当前页码存储在 URL 中。但是您的规则也使用 page 参数并覆盖 Pagination 使用的参数。您应该在分页配置中使用不同的参数,以避免冲突:

'pagination' => [
    'pageSize' => 1,
    'route' => "/".Yii::$app->getRequest()->getQueryParam('page'),
    'pageParam' => 'paginationPage',
]

https://www.yiiframework.com/doc/api/2.0/yii-data-pagination#$pageParam-detail


或者您可以在规则中重命名参数以避免在其他地方也出现此类问题:

'rules' => [
    '<r_page>/<r_category>/<r_product>' => 'splitter/manage-request',
    '<r_page>/<r_category>' => 'splitter/manage-request',
    '<r_page>' => 'splitter/manage-request',
    '' => 'splitter/manage-request'
],