Yii2分页不会更改页面

时间:2018-06-24 13:53:30

标签: pagination yii2 yii2-advanced-app

我的寻呼机未更改为所选页面。当我单击到下一页按钮时,它将重新加载页面。我也尝试了没有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);
    }
}

网址管理器规则:

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

1 个答案:

答案 0 :(得分:1)

似乎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'
],