如何在paginationControl中使用参数指定路由? [英] How to specify route with parameter in paginationControl?

查看:72
本文介绍了如何在paginationControl中使用参数指定路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在新闻源上创建分页.我的新闻提要中有两种模式:所有新闻提要和按类别提要.所有新闻提要的分页都可以正常工作,但是按类别提要"分页存在问题.

I am trying to create pagination on my news feed. I have two modes in my news feed: all news feed and feed by category. Pagination for all news feed works fine, but I have problems with "feed by category" pagination.

我这样使用paginationControl:

I use paginationControl like this:

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

路由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' => 'News\Controller\Item',
            'action'     => 'category',
            'page'      => 1,
        )
    ),
    'may_terminate' => true,
),

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

So I need to specify parameter category. I am trying this:

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

但是我收到错误消息"Missing parameter ...":

But I am getting error "Missing parameter ...":

似乎无法通过paginationControl设置参数.

Looks like it is impossible to set parameter through paginationControl.

如何正确地在paginationControl中使用参数指定路由?

How to specify route with parameter in paginationControl correctly?

我的paginationControl视图如下:

My paginationControl view looks like:

<?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

You should pass all additional parameters as an associative array at the end like @rianattow's said. In this way, all that parameters will be accessible in your pagination_control.phtml partial via $this->paramname

示例:

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

此详细信息在分页器文档中进行了说明:

第四个也是最后一个参数是为可选的关联保留的 您希望在视图中可用的其他变量数组 (可通过$ this获得).例如,这些值可能包括额外的 分页链接的URL参数.

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,如下所示:

I think other missing point is building the URL's using route names. Instead of passing an url to the paginationControl() helper as an argument, try to generate url's inside the pagination partial like this:

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

希望有帮助.

这篇关于如何在paginationControl中使用参数指定路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆