代码之家  ›  专栏  ›  技术社区  ›  Toma Tomov

YII2分页不会改变页面

  •  1
  • Toma Tomov  · 技术社区  · 6 年前

    我的寻呼机不会更改为所选页面。当我单击“下一页”按钮时,它会重新加载该页。我也试过没有 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'
                ],
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   rob006    6 年前

    看起来像 Pagination PARAM与URL规则冲突。 分页 使用 page 获取param以在url中存储当前页码。但你的规则也使用 第页 param和overrides param由 分页 是的。您应该在分页配置中使用不同的参数,以避免冲突:

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