Php 如何在paginator中使用$\u GET?
我在我的项目中添加了按select->order by$GET变量排序的数据。但是当我通过paginator浏览页面时,当然这些变量不会传递到下一页。传递此变量并与paginator一起使用的最佳方法是什么 控制器:Php 如何在paginator中使用$\u GET?,php,get,zend-framework2,zend-paginator,Php,Get,Zend Framework2,Zend Paginator,我在我的项目中添加了按select->order by$GET变量排序的数据。但是当我通过paginator浏览页面时,当然这些变量不会传递到下一页。传递此变量并与paginator一起使用的最佳方法是什么 控制器: public function indexAction() { $sortForm = new \Records\Form\SortingForm(); $field = 'date'; $order = 'desc'; $request = $th
public function indexAction()
{
$sortForm = new \Records\Form\SortingForm();
$field = 'date';
$order = 'desc';
$request = $this->getRequest();
if ($request->isGet()){
$sortForm->setValidationGroup(array('field', 'order'));
$sortData = $request->getQuery()->toArray();
$sortForm->setData($sortData);
if($sortForm->isValid()) {
$sortForm->getData($sortData);
$field = (string) $this->params()->fromQuery('field', 'date');
$order = (string) $this->params()->fromQuery('order', 'desc');
}
}
$query = $this->getRecordsTable()->fetchAll($field, $order);
$paginator = new Paginator\Paginator(new Paginator\Adapter\Iterator($query));
$paginator->setCurrentPageNumber($this->params()->fromRoute('page', 1));
$paginator->setItemCountPerPage(25);
$vm = new ViewModel(array('records' => $paginator));
型号:
public function fetchAll($field, $order)
{
$this->field = $field;
$this->order = $order;
$resultSet = $this->tableGateway->select(function (Select $select) {
$select->columns(array('date', 'name', 'email', 'homepage', 'text', 'image', 'file'));
$select->order($this->field.' '.$this->order);
});
$resultSet->buffer();
$resultSet->next();
return $resultSet;
}
表格:
分页器视图:
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?php echo $this->url($this->route, array('page' => $page)); ?>">
<?php echo $page; ?>
</a> |
<?php else: ?>
<?php echo $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
还有一个:当我传递get变量时,查询字符串看起来像&submit=Go我怎么能不发送pair submit=>value
谢谢你的帮助!: 首先,您需要确保为分页器使用的路由具有查询字符串子路由,我建议使用一个专门用于分页列表的路由,例如:
'router' => array(
'routes' => array(
'paginator_route' => array(
'type' => 'segment',
'options' => array(
'route' => '/list/:controller/:action[/page/:page][/]',
'constraints' => array(
//'__NAMESPACE__' => 'Application\Controller',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'page' => '[0-9]+',
),
'defaults' => array(
'controller' => 'index',
'action' => 'list',
'page' => 1,
),
),
'may_terminate' => true, // with or without Query string
'child_routes' => array(
'query' => array( // allows us to match query string
'type' => 'Query',
'options' => array(
'defaults' => array(
// Query string defaults here..
)
)
),
),
),
您现在需要修改分页视图,以允许我们在生成新链接时使用当前参数作为基础:
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<?php // the last param allows us to reuse the matched params ?>
<a href="<?php echo $this->url($this->route, array('page' => $page), TRUE); ?>">
<?php echo $page; ?>
</a> |
<?php else: ?>
<?php echo $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
分页链接现在应该保留查询字符串值:-您也可以创建一个帮助器,将GET参数传递给url。我已经为此创建了一个示例模块:和另一个模块:省略提交“name”元素尝试查看这里@Waygood谢谢您的回答。Zend\Form\Fieldset::add:未命名提供的元素或字段集,且标志中未提供名称-如果省略“名称”。如果关于链接-没有帮助…谢谢,你的博客帮了我很多-也许你又帮了我一次:谢谢,它工作了!但有一些问题:1当我现在通过paginator传递页面时,我的查询字符串看起来像/page/3/?field=date&order=asc&controller=Records\controller\Records&action=index。但是,如果我想从url中隐藏“controller”和“action”变量,该怎么办?我的路由器:'route'=>'[/page/:page][/],'defaults'=>array'controller'=>'Records\controller\Records','action'=>'index','page'=>1,在五月终止>>查询>>选项:'defaults'=>array'field'=>'date',“order'=>“desc.”和2是否可以将/page/3/?field=date&order=asc重写为/page/3/date/asc,而不进行mod_重写,但php/zf2?非常感谢您您不能这样重写它们,因为这样它们就成为路由段的一部分,不再是查询字符串,如果您希望它们像那样显示,那么只需更改路由来管理它们。再次感谢。如何更改路径以像/../?一样管理它们?关于我的第一个问题,我可以隐藏一些变量以避免在url中分解吗?
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<?php // the last param allows us to reuse the matched params ?>
<a href="<?php echo $this->url($this->route, array('page' => $page), TRUE); ?>">
<?php echo $page; ?>
</a> |
<?php else: ?>
<?php echo $page; ?> |
<?php endif; ?>
<?php endforeach; ?>