加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何在paginationControl中指定带参数的路由?

发布时间:2020-12-13 21:52:00 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试在我的新闻源上创建分页.我的新闻Feed中有两种模式:所有新闻Feed和按类别提供的Feed.所有新闻Feed的分页工作正常,但我有“按类别分类”分页的问题. 我像这样使用paginationControl: ?=$this-paginationControl( $this-news,'Sliding','paginatio
我正在尝试在我的新闻源上创建分页.我的新闻Feed中有两种模式:所有新闻Feed和按类别提供的Feed.所有新闻Feed的分页工作正常,但我有“按类别分类”分页的问题.

我像这样使用paginationControl:

<?=$this->paginationControl(
    $this->news,'Sliding','pagination_control',array('route' => 'news/category')
)?>

路线新闻/类别配置:

'category' => array(
    'type' => 'Segment','options' => array(
        'route' => '/:category[/page-:page]','constraints' => array(
            'category'     => '[a-z]+','page'     => '[1-9][0-9]*',),'defaults' => array(
            'controller' => 'NewsControllerItem','action'     => 'category','page'      => 1,)
    ),'may_terminate' => true,

所以我需要指定参数类别.我在尝试这个:

<?=$this->paginationControl(                                                                 
    $this->news,array('route' => 'news/category',array('category' => $this->category->getUrl()))        
)?>

但我收到错误“缺少参数…”:

???????????????????????????????????????????????????????????????

看起来无法通过paginationControl设置参数.

如何在paginationControl中正确指定带参数的路由?

更新1

我的paginationControl视图如下所示:

<?php if ($this->pageCount > 1): ?>
    <div>
        <ul class="pagination pagination-sm">
            <!-- Previous page link -->
            <?php if (isset($this->previous)): ?>
                <li>
                    <a href="<?=$this->url($this->route,array('page' => $this->previous))?>">
                        &laquo;
                    </a>
                </li>
            <? else: ?>
                <li class="disabled"><span>&laquo;</span></li>
            <? endif; ?>

            <!-- Numbered page links -->
            <? foreach ($this->pagesInRange as $page): ?>
                <? if ($page != $this->current): ?>
                    <li>
                        <a href="<?=$this->url($this->route,array('page' => $page))?>">
                            <?=$page?>
                        </a>
                    </li>
                <? else: ?>
                    <li class="active"><span><?=$page?></span></li>
                <? endif; ?>
            <? endforeach; ?>

            <!-- Next page link -->
            <?php if (isset($this->next)): ?>
                <li>
                    <a href="<?=$this->url($this->route,array('page' => $this->next))?>">
                        &raquo;
                    </a>
                </li>
            <?php else: ?>
                <li class="disabled"><span>&raquo;</span></li>
            <?php endif; ?>
        </ul>
    </div>
    <div>
        <span class="small grey"><?="Страница ".$this->current." из ".$this->pageCount?></span>
    </div>
<?php endif; ?>

解决方法

您应该将所有其他参数作为关联数组传递到最后,如@ rianattow所说.通过这种方式,所有参数都可以通过$this-> paramname在pagination_control.phtml部分中访问

例:

echo $this->paginationControl(                                                                 
     $this->news,array( 'route' => 'news/category','category' => 'banana')
    );

这个细节在paginator documentation中说明:

The fourth and final parameter is reserved for an optional associative
array of additional variables that you want available in your view
(available via $this). For instance,these values could include extra
URL parameters for pagination link.

我认为其他缺点是使用路由名称构建URL.不要将url作为参数传递给paginationControl()助手,而是尝试在分页部分内生成url,如下所示:

<a href="<?
    echo $this->url($this->route,array('page' => $page,'category' => $this->category))
?>">

希望能帮助到你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读