PHP code example of tiloweb / pagination-bundle
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
return [
// ...
Tiloweb\PaginationBundle\TilowebPaginationBundle::class => ['all' => true],
];
namespace App\Controller;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Tiloweb\PaginationBundle\Service\Paginator;
class UserController extends AbstractController
{
#[Route('/users', name: 'app_users')]
public function index(
Request $request,
UserRepository $userRepository,
Paginator $paginator,
): Response {
$queryBuilder = $userRepository->createQueryBuilder('u')
->orderBy('u.createdAt', 'DESC');
$pagination = $paginator->paginate(
queryBuilder: $queryBuilder,
page: $request->query->getInt('page', 1),
itemsPerPage: 20,
);
return $this->render('user/index.html.twig', [
'pagination' => $pagination,
]);
}
}
namespace App\Repository;
use App\Entity\Article;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Tiloweb\PaginationBundle\Service\PaginationResult;
use Tiloweb\PaginationBundle\Service\Paginator;
/**
* @extends ServiceEntityRepository<Article>
*/
class ArticleRepository extends ServiceEntityRepository
{
public function __construct(
ManagerRegistry $registry,
private readonly Paginator $paginator,
) {
parent::__construct($registry, Article::class);
}
/**
* @return PaginationResult<Article>
*/
public function findPublishedPaginated(int $page = 1, int $limit = 10): PaginationResult
{
$qb = $this->createQueryBuilder('a')
->where('a.published = :published')
->setParameter('published', true)
->orderBy('a.publishedAt', 'DESC');
return $this->paginator->paginate($qb, $page, $limit);
}
}