PagerfantaBundle

repository·4.x·Indexed 42 days ago

https://github.com/babdev/pagerfantabundle

A Symfony bundle that integrates the Pagerfanta pagination library into the Symfony framework. It provides Twig integration, support for custom Pagerfanta views, serialization via Symfony or JMS Serializer, and flexible route generation for paginated datasets.

Tokens
7.4K
Snippets
20
Records
32
Agent score
52%

What's inside PagerfantaBundle

  1. Overview of PagerfantaBundle features

    4.x

    PagerfantaBundle integrates the Pagerfanta library into Symfony applications. It provides several key features to handle pagination in a Symfony-native way:

    • Twig Integration: A Twig function to render Pagerfanta objects using specific views and options.
    • Pagerfanta Views: Support for Twig templates within Pagerfanta views.
    • Pagination Services: Services to utilize Pagerfanta views throughout your application.
    • Serialization: Support for serializing Pagerfanta\PagerfantaInterface objects using either the Symfony Serializer component or the JMS Serializer.
    • Routing Customization: A flexible API for customizing how paginated routes are handled.
    • Default Styling: Includes default CSS for the Pagerfanta\View\DefaultView class.
  2. How to use a mutable view factory in PagerfantaBundle 4.0

    4.x

    In version 4.0, the pagerfanta.view_factory service is now an instance of BabDev\PagerfantaBundle\View\ContainerBackedImmutableViewFactory, making it immutable at runtime.

    If your implementation requires a mutable view factory, you must replace the pagerfanta.view_factory service definition in your container configuration before the BabDev\PagerfantaBundle\DependencyInjection\CompilerPass\RegisterPagerfantaViewsPass compiler pass is executed.

  3. How paginated routes are generated

    4.x

    When rendering a Pagerfanta view, a route generator is required to create URLs for each item in the pagination list. The system relies on two main interfaces:

    1. Pagerfanta\RouteGenerator\RouteGeneratorInterface: The object responsible for the actual route generation.

      • Default implementation: BabDev\PagerfantaBundle\RouteGenerator\RouterAwareRouteGenerator (uses Symfony Routing).
    2. Pagerfanta\RouteGenerator\RouteGeneratorFactoryInterface: A factory service used to create a RouteGeneratorInterface at runtime.

      • Default implementation: BabDev\PagerfantaBundle\RouteGenerator\RequestAwareRouteGeneratorFactory (uses the current Symfony Request object to determine default route names and parameters).

    The Twig integration uses a RouteGeneratorFactoryInterface instance to create the generator used during view rendering.

  4. How to add custom Pagerfanta views

    4.x

    Views are registered in the Symfony service container using the pagerfanta.view tag.

    When registering a view, you can optionally provide an alias. This alias becomes the name used to retrieve the view via a Pagerfanta\View\ViewFactoryInterface instance or within Twig. If no alias is provided, the service ID is used as the name.

    Best Practice: It is recommended that view services are NOT public. Instead, use the Pagerfanta\View\ViewFactoryInterface to retrieve them.

  5. Install PagerfantaBundle via Composer

    4.x

    Install the bundle using Composer. Note that this bundle only installs the pagerfanta/core package, which provides the core Pagerfanta API. You must manually install the specific adapters required for your project (e.g., Doctrine, Eloquent, etc.) following the official Pagerfanta documentation.

    composer require babdev/pagerfanta-bundle
  6. Load default view CSS

    4.x

    The bundle includes basic CSS for the default view. You can include it in your Twig templates using the asset() function. If you are using Symfony's AssetMapper component, you should follow the standard Symfony documentation for importing assets from third-party bundles.

    <link rel="stylesheet" href="{{ asset('bundles/babdevpagerfanta/css/pagerfanta.css') }}">
  7. Serialize Pagerfanta instances using JMS Serializer

    4.x
    PagerfantaBundle supports serialization via the JMS Serializer. To enable this, you must have the JMSSerializerBundle installed in your project. The bundle will then provide the necessary serialization handler to process Pagerfanta\Pagerfanta instances.
  8. Pass Pagerfanta to Twig templates

    4.x

    To render pagination in a Symfony application, you must first instantiate a Pagerfanta\Pagerfanta object in your controller and pass it to your Twig template via the render context. Typically, you wrap a Doctrine QueryBuilder with a QueryAdapter before passing it to the Pagerfanta constructor.

    <?php
    
    namespace App\Controller;
    
    use App\Entity\BlogPostRepository;
    use Pagerfanta\Doctrine\ORM\QueryAdapter;
    use Pagerfanta\Pagerfanta;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Annotation\Route;
    
    final class BlogController extends AbstractController
    {
        #[Route(path: '/blog', name: 'app_blog_list', methods: ['GET'])]
        public function listPosts(BlogPostRepository $blogPostRepository): Response
        {
            $queryBuilder = $blogPostRepository->createBlogListQueryBuilder();
    
            $pagerfanta = new Pagerfanta(
                new QueryAdapter($queryBuilder),
            );
    
            return $this->render(
                'blog/list.html.twig',
                [
                    'pager' => $pagerfanta,
                ],
            );
        }
    }
  9. Use Twig templates for Pagerfanta views

    4.x

    To render pagination using Twig, you must first install the pagerfanta/twig package. The bundle provides pre-configured Twig templates for various CSS frameworks. You can use these templates to ensure your pagination matches your application's styling. The 'Previous' and 'Next' button labels are localizable within these templates.

    | Template Name                                    | Framework                                                     |
    |--------------------------------------------------|---------------------------------------------------------------|
    | `@BabDevPagerfanta/default.html.twig`            | None (Pagerfanta's default view)                             |
    | `@BabDevPagerfanta/foundation6.html.twig`         | [Foundation](https://get.foundation/index.html) (version 6.x) |
    | `@BabDevPagerfanta/semantic_ui.html.twig`        | [Semantic UI](https://semantic-ui.com) (version 2.x)          |
    | `@BabDevPagerfanta/tailwind.html.twig`           | [Tailwind CSS](https://tailwindcss.com/)                      |
    | `@BabDevPagerfanta/twitter_bootstrap.html.twig`  | [Bootstrap](https://getbootstrap.com) (version 2.x)           |
    | `@BabDevPagerfanta/twitter_bootstrap3.html.twig` | [Bootstrap](https://getbootstrap.com) (version 3.x)           |
    | `@BabDevPagerfanta/twitter_bootstrap4.html.twig` | [Bootstrap](https://getbootstrap.com) (version 4.x)           |
    | `@BabDevPagerfanta/twitter_bootstrap5.html.twig` | [Bootstrap](https://getbootstrap.com) (version 5.x)           |