gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 22:11:21 24 4
gpt4 key购买 nike

我正在尝试在我的新闻提要上创建分页。我的新闻提要有两种模式:所有新闻提要和按类别提要。所有新闻提要的分页工作正常,但我在“按类别提要”分页时遇到问题。

我这样使用 paginationControl:

<?=$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,
),

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

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

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

enter image description here

貌似无法通过paginationControl设置参数。

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

更新1

我的 paginationControl View 如下所示:

<?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,
'Sliding',
'pagination_control',
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))
?>">

希望对您有所帮助。

关于php - 如何在 paginationControl 中指定带有参数的路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25146124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com