Symfony pagination

routing.yml

index_offer:
url
: /offer/index/page/:page
param
: { module: offers, action: index, page: 1} #set default value of page to 1

actions.class.php

public function preExecute()
{
$culture = $this->getUser()->getCulture();

$siteSectionId = SiteSectionTable::getSiteSectionId($culture, 'offer');
$this->siteSectionId = $siteSectionId;

return parent::preExecute();
}

public function executeIndex(sfWebRequest $request)
{
$numPerPage = sfConfig::get('app_max_offers_on_offer_index');
$query = OfferTable::getAllAvailableOffersQuery($this->siteSectionId);

$this->processPager($request, $numPerPage, $query);
}

protected function processPager(sfWebRequest $request, $numPerPage, $query)
{
$this->pager = new sfDoctrinePager('Offer', $numPerPage);
$this->pager->setQuery($query);
$this->pager->setPage($request->getParameter('page', 1));
$this->pager->init();
}

_pagination.php

<?php if ($pager->haveToPaginate()): ?>
<div class="pagination <?php echo is_null($sub_class)?null:$sub_class; ?>">
<?php if ($sub_class == 'endpage'): ?>
<span>Page <?php echo $pager->getPage(); ?> sur <?php echo $pager->getLastPage(); ?></span>
<?php endif; ?>

<a href="<?php echo url_for($route, array('page' => '1')); ?>">première</a>
<a href="<?php echo url_for($route, array('page' => $pager->getPreviousPage())); ?>">précédente</a>
<ul>
<?php foreach ($pager->getLinks() as $page): ?>
<?php if ($page == $pager->getPage()): ?></a></li>
<li><a class="actif"><?php echo $page; ?>
<?php else: ?>
<li><a href="<?php echo url_for($route, array('page' => $page)); ?>"><?php echo $page; ?></a></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<a href="<?php echo url_for($route, array('page' => $pager->getNextPage())); ?>">suivante</a>
<a href="<?php echo url_for($route, array('page' => $pager->getLastPage())); ?>">dernière</a>
</div>
<?php endif; ?>

posted @ 2011-08-31 20:37  Lux.Y  阅读(451)  评论(0)    收藏  举报