Skip to content

Instantly share code, notes, and snippets.

@smottt
Created November 27, 2011 17:19
Show Gist options
  • Save smottt/1397845 to your computer and use it in GitHub Desktop.
Save smottt/1397845 to your computer and use it in GitHub Desktop.
PagerfantaBundle Symfony2 Controller Example
<?php
use Symfony\Bundle\FrameworkBundle\Controller\Controller,
Symfony\Component\HttpFoundation\Request,
Sensio\Bundle\FrameworkExtraBundle\Configuration\Method,
Sensio\Bundle\FrameworkExtraBundle\Configuration\Route,
Sensio\Bundle\FrameworkExtraBundle\Configuration\Template,
Pagerfanta\Pagerfanta,
Pagerfanta\Adapter\DoctrineORMAdapter,
Pagerfanta\Exception\NotValidCurrentPageException;
class ExampleController extends Controller
{
/**
* @Route("/example/{page}",
* name="example",
* requirements={"page" = "\d+"},
* defaults={"page" = "1"}
* )
* @Template()
*
* @param int $page
*/
public function exampleAction($page)
{
$repo = $this->getDoctrine()->getRepository('AcmeExampleBundle:Example');
// returns \Doctrine\ORM\Query object
$query = $repo->getExampleQuery();
$pagerfanta = new Pagerfanta(new DoctrineORMAdapter($query));
$pagerfanta->setMaxPerPage(45);
try {
$pagerfanta->setCurrentPage($page);
} catch(NotValidCurrentPageException $e) {
throw new NotFoundHttpException();
}
return ['examples' => $pagerfanta];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment