我正在使用Zend 2.3构建网站,我想要做的是使用Zegin 2.3对结果进行分页 Zend Paginator。我没有收到任何错误,事实上我可以正确地看到每个链接,但是它们无法正常工作。
当我想在一个类别中对新闻进行分页时,Paginator会生成以下链接:
........./index/categories/1
........./index/categories/2
........./index/categories/3
............................
相反,我想要的链接(即类别2)应该是:
............/index/categories/2/1
............/index/categories/2/2
............/index/categories/2/3
这是我的module.config:
'index'=>array(
'type'=>'Segment',
'options'=>array(
'route' => '/index[/[:action][/:id][/:id2]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index'
),
),
),
我已经尝试将$ cat_id从控制器传递给网址助手(' id' => $ this-> id),但它不起作用。当我对id值进行硬编码时,它似乎才起作用,例如:
<a href="<?php echo $this->url('index', array('action'=>'categories','id'=>3,'id2' => $this->first)); ?>">First </a>
这不起作用:
<a href="<?php echo $this->url('index', array('action'=>'categories','id'=>$this->id,'id2' => $this->first)); ?>">First </a>
所以我的问题是,我怎样才能将变量传递给Paginator?
由于
答案 0 :(得分:0)
很难用如此有限的信息创建一个例子,但通常你会这样做:
这将在你的控制器中
// This is some array of data that you've pulled from a data source
$arrayOfYourData = array();
$paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($arrayOfYourData));
$paginator->setItemCountPerPage(25);
$paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page',1));
// now return $paginator to your view by assigning it to ViewModel, etc
然后在视图中,你会有类似这样的例子
<?php echo $this->paginationControl($paginator, 'Sliding', 'your-namespace/pagination_control.phtml', array('route'=>'some/route', 'routeOptions'=>array('action'=>'some-action')); ?>
然后你应该有一个views / your-namespace / pagination_control.phtml:
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<?php echo $this->firstItemNumber; ?> - <?php echo $this->lastItemNumber; ?>
of <?php echo $this->totalItemCount; ?>
<!-- First page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url($this->route, array('page' => $this->first)); ?>">
First
</a> |
<?php else: ?>
<span class="disabled">First</span> |
<?php endif; ?>
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url($this->route, array('page' => $this->previous)); ?>">
< Previous
</a> |
<?php else: ?>
<span class="disabled">< Previous</span> |
<?php endif; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url($this->route, array('page' => $this->next)); ?>">
Next >
</a> |
<?php else: ?>
<span class="disabled">Next ></span> |
<?php endif; ?>
<!-- Last page link -->
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url($this->route, array('page' => $this->last)); ?>">
Last
</a>
<?php else: ?>
<span class="disabled">Last</span>
<?php endif; ?>
</div>
<?php endif; ?>