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

Knp Components Laravel Package

knplabs/knp-components

Knp Components is a PHP component library from KnpLabs. It currently includes the Pager component for building flexible, “fancy” pagination and paging adapters. PHPUnit 10/11 supported; install with Composer and run the test suite via composer test.

View on GitHub
Deep Wiki
Context7

Intro to Knp Pager Component

This is a PHP 8 paginator with a totally different core concept.

How is it different? First of all, it uses Symfony's event dispatcher to paginate whatever is needed. The pagination process involves triggering events which hit the subscribers. If the subscriber knows how to paginate the given object, it does. Finally, some subscriber must initialize the pagination view object, which will be the result of pagination request. Pagination view can be anything which will be responsible for how to render the pagination.

Magic? no! only KISS principle

Why reinvent the wheel? Can someone tell me what's the definition of wheel in the software world?

Requirements:

  • Symfony EventDispatcher component
  • Symfony HttpFoundation component (optional, if you want to retrieve data from Symfony Request)
  • Namespace based autoloader for this library

Features:

  • Can be customized in any way needed, etc.: pagination view, event subscribers.
  • Possibility to add custom filtering, sorting functionality depending on request parameters.
  • Pagination view extensions based on event.
  • Paginator extensions based on events, etc.: another object pagination compatibilities.
  • Supports multiple paginations during one request
  • Separation of concern, paginator is responsible for generating the pagination view only, pagination view - for representation purposes.
  • Does not require initializing specific adapters
  • configurable

Usage examples:

Controller

// see usage.md for full vars
$dispatcher = '[..]';   
$accessor = '[..]';
$paginator = new Knp\Component\Pager\Paginator($dispatcher, $accessor);
$target = range('a', 'u');
// uses event subscribers to paginate $target
$pagination = $paginator->paginate($target, 2/*page*/, 10/*limit*/);

// iterate paginated items
foreach ($pagination as $item) {
    //...
}
echo $pagination; // renders pagination

// overriding view rendering

$pagination->renderer = function ($data) use ($template) {
    return $twig->render($template, $data);
};

echo $pagination;

Doctrine query pagination

Easy paginate over Doctrine ORM query:

$pagination = $paginator->paginate($em->createQuery('SELECT a FROM Entity\Article a'), 1/*page*/, 10/*limit*/);

Custom data repository pagination

For applications having custom data repositories (like DDD repositories, CQRS read models) you can provide custom data retrieval inside callbacks.

$repository = ...;

$count = function () use ($repository) {
    // your $repository invocation here
};
$items = function ($offset, $limit) use ($repository) {
    // your $repository invocation here
};
$target = new CallbackPagination($count, $items);
$pagination = $paginator->paginate($target, 2/*page*/, 10/*limit*/);

// ...
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.
bugban/symfony
beyonder-capi/workflow-extensions-bundle
beyonder-capi/job-queue-bundle
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
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