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

Laminas Paginator Laravel Package

laminas/laminas-paginator

Laminas Paginator provides flexible pagination for PHP apps, with adapters for arrays, iterators, and database results. Generate page ranges and navigation data, and integrate with Laminas MVC or use standalone for paged listings.

View on GitHub
Deep Wiki
Context7

Caching Paginator Pages

It is possible to cache pagination results by wrapping adapters in the shipped CachingAdapter. The CachingAdapter requires a pagination adapter, a PSR cache item pool and a unique cache-key prefix.

use DateInterval;
use Laminas\Paginator\Adapter\AdapterInterface;
use Psr\Cache\CacheItemPoolInterface;

final readonly class CachingAdapter implements AdapterInterface
{
    /**
     * [@param](https://github.com/param) AdapterInterface<TKey, TValue> $adapter
     * [@param](https://github.com/param) non-empty-string $cacheKeyPrefix
     */
    public function __construct(
        private AdapterInterface $adapter,
        private string $cacheKeyPrefix,
        private CacheItemPoolInterface $cache,
        private DateInterval|null $ttl,
    ) {
    }

    // ...
}

To manually create a paginator that will cache results as they are retrieved, the following would be required:

use Laminas\Paginator\Adapter\AdapterInterface;
use Laminas\Paginator\Adapter\CachingAdapter;
use Laminas\Paginator\Paginator;
use Psr\Cache\CacheItemPoolInterface;

$adapter = new \SpecialCustomAdapter(/** With some data that needs to be cached */);

// Assume we have some ready-prepared objects:
assert($cachePool instanceof CacheItemPoolInterface);
assert($adapter instanceof AdapterInterface);

$paginator = new Paginator(
    new CachingAdapter(
        $adapter,
        'my-unique-prefix',
        $cachePool,
        new DateInterval('PT30M'),
    ),
    10,
    10,
    'Sliding',
);

The above example assumes that \SpecialCustomAdapter is an adapter that makes expensive database calls or other I/O where caching is a useful performance improvement.

The cache pool argument can be any PSR-6 implementation. laminas-cache provides a PSR-6 implementation along with many other libraries.

Paginator assumes that you will likely use a single cache pool for all paginators, therefore a unique key prefix is required to prevent cache-key collisions with other paginator instances used elsewhere in your application.

Finally, you can also provide an optional TTL in the form of a DateInterval object.

PaginatorFactory Integration and Setting Defaults

The shipped PaginatorFactory class is capable of decorating adapters for you, providing some configuration is in-place, thereby reducing the previous example to:

use Laminas\Paginator\PaginatorFactory;
use Psr\Container\ContainerInterface;

assert($container instanceof ContainerInterface);

$adapter = new \SpecialCustomAdapter(/** With some data that needs to be cached */);

$factory = $container->get(PaginatorFactory::class);
$paginator = $factory->withCachingAdapter($adapter, 'my-unique-prefix');

Using the factory assumes that a single cache item pool will be used for all paginators (Which is why the unique prefix is necessary). To take advantage of the paginator factory, you must ensure that you configure the necessary services. The following example assumes that you are using Laminas Service Manager for dependency injection.

return [
    // Laminas Service Manager Configuration:
    'dependencies' => [
        'factories' => [
            'PaginatorCachePool' => \SomeFactoryThatWillProduceACacheItemPoolInterface::class,
        ],
    ],
    
    // Telling the paginator factory which "Service" to pull from the container:
    'paginator' => [
        'defaultCache' => 'PaginatorCachePool',
        'defaultCacheTTL' => 'PT30M',
    ],
];

Further information:

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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport