Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Pager Bundle Laravel Package

anh/pager-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the bundle via Composer:

    composer require anh/pager-bundle
    

    Register the bundle in config/bundles.php (Symfony 4+):

    Anh\PagerBundle\AnhPagerBundle::class => ['all' => true],
    
  2. Basic Usage Inject the Pager service into your controller:

    use Anh\PagerBundle\Pager\Pager;
    
    class ProductController extends AbstractController
    {
        public function index(Pager $pager)
        {
            $products = $this->getDoctrine()
                ->getRepository(Product::class)
                ->findAll();
    
            $pagination = $pager->paginate($products, $this->get('request')->query->get('page', 1), 10);
            return $this->render('product/index.html.twig', [
                'products' => $pagination->getItems(),
                'pagination' => $pagination,
            ]);
        }
    }
    
  3. First Use Case Display paginated results in a Twig template:

    {% for product in products %}
        {{ product.name }}
    {% endfor %}
    
    {{ pagination|pager_bundle_pagination() }}
    

Implementation Patterns

Common Workflows

  1. Dynamic Pagination Pass request parameters directly:

    $page = $request->query->get('page', 1);
    $limit = $request->query->get('limit', 10);
    $pagination = $pager->paginate($items, $page, $limit);
    
  2. Customizing Pagination Views Override Twig templates in templates/AnhPagerBundle/pagination/. Example: Extend pagination.html.twig to add custom buttons.

  3. Integration with Doctrine Use paginateQuery() for database queries:

    $query = $entityManager->createQuery('SELECT p FROM App\Entity\Product p');
    $pagination = $pager->paginateQuery($query, $page, $limit);
    
  4. API Responses Return pagination metadata as JSON:

    return $this->json([
        'data' => $pagination->getItems(),
        'meta' => [
            'current_page' => $pagination->getCurrentPage(),
            'total_pages' => $pagination->getTotalPages(),
        ],
    ]);
    

Best Practices

  • Cache Pagination Metadata: Store total_items in cache if queries are expensive.
  • Default Values: Set defaults in config/packages/anh_pager.yaml:
    anh_pager:
        default_items_per_page: 15
        default_max_per_page: 50
    
  • AJAX Support: Use pager_bundle_ajax_pagination Twig filter for seamless updates.

Gotchas and Tips

Pitfalls

  1. Deprecated Dependency The bundle depends on sp/bower-bundle (v0.9), which is outdated. Ensure compatibility with your frontend setup.

  2. Missing Documentation No official docs exist; rely on:

    • src/Resources/views/ for Twig templates.
    • PagerInterface for method signatures.
  3. Query Pagination Quirks paginateQuery() assumes Doctrine queries. For raw SQL, use paginate() with fetched results.

  4. Twig Filter Conflicts The pager_bundle_pagination filter may clash with other bundles. Prefix templates if needed:

    {% include 'AnhPagerBundle::pagination/custom.html.twig' %}
    

Debugging Tips

  • Check Pagination Object:
    dump($pagination->getIterator()->count());
    
  • Log Query Parameters:
    $this->logger->debug('Pagination params:', [
        'page' => $page,
        'limit' => $limit,
        'total' => count($items),
    ]);
    

Extension Points

  1. Custom Pagination Classes Implement Anh\PagerBundle\Pager\PaginationInterface for custom logic:

    class CustomPagination implements PaginationInterface
    {
        public function getItems() { ... }
        // ... other methods
    }
    
  2. Override Templates Copy templates/AnhPagerBundle/pagination/ to your project and modify:

    • pagination.html.twig (main layout).
    • slots/prev.html.twig (custom prev/next buttons).
  3. Add CSS/JS Include assets via sp/bower-bundle or manually link:

    {{ encore_entry_link_tags('app') }}
    <link rel="stylesheet" href="{{ asset('bundles/anhpager/css/pager.css') }}">
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
craftcms/url-validator
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony