Created
November 27, 2011 17:19
-
-
Save smottt/1397845 to your computer and use it in GitHub Desktop.
PagerfantaBundle Symfony2 Controller Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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