osw3/symfony-pagination

OSW3 Pagination Bundle for Symfony

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 1

Open Issues: 0

Type:symfony-bundle

1.0.6 2023-12-02 02:09 UTC

This package is auto-updated.

Last update: 2024-03-31 02:55:55 UTC


README

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() }}