Skip to content

OSW3/symfony-pagination

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

Pagination

Add simple pagination to Symfony projects

Install

Instal the bundle

comoser require osw3/symfony-pagination

Prepare for update

In your composer.json file, change the line of the dependency to prepare futures updates of the bundle.

"osw3/symfony-pagination": "*",

Enable the bundle

Add the bundle in the config/bundle.php file.

return [
    OSW3\Pagination\PaginationBundle::class => ['all' => true],
];

Usage

1. in the controller

#[Route('/', name: 'app_book_index', methods: ['GET'])]
public function index(BookRepository $bookRepository, PaginationService $paginationService): Response
{
    /// 1. Set up the Paging 
    /// --
    
    // A. Set the repository to the service
    $repository = $paginationService->setRepository($bookRepository);

    // B. (optional) Set the repository method
    $repository->setMethod('findBy');
    

    /// 2. Find entities
    /// --

    // A. Find entities with optional sorter
    $books = $repository->find(['title' => 'ASC']);

    // B. or Find entities based on criteria and optional sorter
    // $books = $repository->findBy(
    //     ['author' => "John"],
    //     ['title' => 'ASC']
    // );


    /// 3. Render the view
    /// --

    // Render the view with books list and the pagination service
    return $this->render('book/index.html.twig', [
        'paginationService' => $paginationService,
        'books' => $books,
    ]);
}

2. in Twig view

Shows paging buttons

{{ pagination({
    paginationService: paginationService,
    route: 'app_book_index',
    absolute: true
}) }}

Shows the total number of items

{{ pagination_total() }}

Shows the total number of pages

{{ pagination_pages() }}

Shows the current page number

{{ pagination_page() }}

Shows the number of items per page

{{ pagination_per_page() }}

About

Add simple pagination to Symfony projects

Resources

Stars

Watchers

Forks

Packages

No packages published