Implementation of pagination into Lumberjack
- PHP 8.0 or greater
- Composer
- Lumberjack
composer require agence-adeliom/lumberjack-pagination
Add the trait into your post type
<?php
namespace App\PostTypes;
use Rareloop\Lumberjack\Post as BasePost;
use Adeliom\Lumberjack\Pagination\Pagination;
class Post extends BasePost
{
use Pagination;
}
Now your can use it into your controllers
<?php
namespace App;
use Adeliom\Lumberjack\Pagination\PaginationViewModel;
use App\Http\Controllers\Controller;
use Rareloop\Lumberjack\Http\Responses\TimberResponse;
use App\PostTypes\Post;
use Timber\Timber;
class IndexController extends Controller
{
public const RESULTS_PER_PAGE = 10;
public function handle(): TimberResponse
{
global $paged;
if (!isset($paged) || !$paged) {
$paged = 1;
}
$context = Timber::get_context();
$context['posts'] = Post::paginate(self::RESULTS_PER_PAGE);
$context['pagination'] = PaginationViewModel::fromQueryBuilder(self::RESULTS_PER_PAGE, $paged);
return new TimberResponse(['page/index.html.twig'], $context);
}
}
Lumberjack Pagination is released under the MIT License.