How to specify route with parameter in paginationC

2019-06-28 05:11发布

问题:

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.

I use paginationControl like this:

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

Route news/category config:

'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()))        
)?>          

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

Looks like it is impossible to set parameter through paginationControl.

How to specify route with parameter in paginationControl correctly?

Update 1

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

回答1:

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

Example:

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

This detail stated in 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.

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

Hope it helps.