1. Go to this page and download the library: Download tiloweb/pagination-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tiloweb / pagination-bundle example snippets
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Tiloweb\PaginationBundle\TilowebPaginationBundle(),
);
// ...
}
// ...
}
// Bundle/Entity/Repository/User.php
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class User extends EntityRepository
{
public function findByPage($page = 1, $max = 10)
{
$dql = $this->createQueryBuilder('user');
$dql->orderBy('user.lastname', 'DESC');
$firstResult = ($page - 1) * $max;
$query = $dql->getQuery();
$query->setFirstResult($firstResult);
$query->setMaxResults($max);
$paginator = new Paginator($query);
if(($paginator->count() <= $firstResult) && $page != 1) {
throw new NotFoundHttpException('Page not found');
}
return $paginator;
}
}
// Bundle/Controller/DefaultController.php
namespace Bundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* @Route("/user/", name="app_list_user")
*/
public function listUserAction(Request $request)
{
$db = $this->getDoctrine()->getManager();
$listUser = $db->getRepository('AppBundle:User')->findByPage(
$request->query->getInt('page', 1),
5
);
return $this->render('listUser.html.twig', array(
'listUser' => $listUser
));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.