3

I'm new to ZF2 and I'm willing to share how I do to retain parameter from form using url helper especially during pagination. I modify the answer from How can you add query parameters in the ZF2 url view helper

This is what I do:

AlbumController.php

// get all the query from url
$input = $form->getData();

$paginator = $this->getAlbumTable()->fetchAll();
$paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));
$paginator->setItemCountPerPage(30);

// unset the 'page' query if necessary
unset($input['page']);

return array(
    'form'   => $form,
    'paginator' => $paginator,
    'routeParams' => array_filter($input) // filter empty value
);

index.phtml

echo $this->paginationControl(
    $this->paginator,
    'sliding',
    array('partial/paginator.phtml', 'Album'),
    array(
        'route' => 'album',
        'routeParams' => $routeParams
    )
);

paginator.phtml

<a href="<?php echo $this->url(
                    $this->route, // your route name
                    array(),      // any url options, e.g action
                    array('query' => $this->routeParams) // your query params
               ); 
echo (empty($this->routeParams))?  '?' : '&'; ?>
page=<?php echo $this->next; ?>">Next Page</a>

Please provide any better solution and correct me if I'm wrong.

Thank you

4

1 回答 1

1

我没有比您更好的解决方案 - 我看不到在添加一些新查询参数时保留现有查询参数的正确方法。但以下内容比手动附加 & 和 = 字符更简洁:

分页器.phtml

<a href="<?php echo $this->url(
    $this->route, // your route name
    array(),      // any url options, e.g action
    // Merge the array with your new value(s)
    array('query' => array('page' => $this->next) + $this->routeParams)
); ?>">Next Page</a>

这也将确保如果您已经有一个page参数,它将被新的参数覆盖。

(从技术上讲,您也可以使用$_GETor$_POST直接避免从控制器传递它,但这似乎不太整洁)

于 2013-08-26T12:24:17.307 回答