ekrouzek/pagination-filters-bundle
Symfony bundle that allows you to set paging, filtering and sorting for selected API endpoints.
To download the most up-to-date stable version of this bundle, open a command console, navigate to your project directory, and execute the following command:
composer require ekrouzek/pagination-filters-bundle
If Flex is being used, the bundle will be automatically enabled. However, if Flex is not being used, manual enabling of the bundle is required by adding the following line to the config/bundles.php file in your project.
<?php
// config/bundles.php
return [
// ...
Ekrouzek\PaginationFiltersBundle\PaginationFiltersBundle::class => ['all' => true],
// ...
];
Usage is shown on an example GET endpoint to get all the courses.
First, it is necessary to specify the query parameters that we expect for the method:
#[QueryParam(name: "page", requirements: "\d+", default: 1)]
#[QueryParam(name: "itemsPerPage", requirements: "\d+", default: 10)]
#[QueryParam(name: "filter", default: "")]
#[QueryParam(name: "sort", default: "")]
Then you also need to add ParamFetcher to the function parameters:
public function getCourses(ParamFetcher $paramFetcher, ...): View
Subsequently, it is enough to add the definition of which data can be filtered and sorted according to which at the same time to the given method:
addNumberField(), addTextField(), addDatetimeField(), addBooleanField() methods are available. The first parameter of these functions is the key that will be presented to the outside - that is, which can be entered into the filter from the outside. The second parameter specifies to which attribute it is mapped in the specified DQL query (more below).
So for example:
$paginationHandler = new PaginationHandler($paramFetcher);
$paginationHandler->createQueryFilter()
->addNumberField("id", "c.id")
->addTextField("semester", "c.semester")
->addTextField("subject", "c.subject")
->addDatetimeField("created", "c.created");
Instead of the already obtained result, it is necessary to pass the DQL QueryBuilder for evaluation. It is therefore necessary to create your own method in the repository for obtaining the query. The idea is the same as if you were to create your own method in the repository, however you don't call ->getQuery()->getResult() at the end, but you pass directly the QueryBuilder.
So for example:
public function getAllCoursesQuery(): QueryBuilder
{
return $this->getEntityManager()
->createQueryBuilder()
->select('c')
->from(Course::class, 'c');
}
Finally, you only create a method in the corresponding Service, which transfers the result of this method in the repository to the controller.
To get the final result of the query, just call ->getPaginatedData($query) in the controller:
$query = $this->courseService->getAllCoursesQuery();
try {
$courses = $paginationHandler->getPaginatedData($query);
} catch (PaginationAndFilterException $exception) {
return $this->sendBadRequest($exception->getMessage());
}
So the result just needs to be serialized and sent.
To send the result with the _pagination header, just call the defined sendPaginatedResponse() method, which wraps the serialized data with a header with pagination information:
// Create response.
$result = $this->courseService->formatMany($courses);
return $paginationHandler->sendPaginatedResponse($result);
To use paging, filtering and paging, just add the corresponding GET parameters to the HTTP request.
page parameter can be used to specify how many pages from the server you want to return.itemsPerPage parameter can be used to specify how many items you want on one page.filter parameter.<method>:<field>:<value>.eq, neq, like, not-like, lt, lte, gt, gte.& (AND) and | (OR).( and ).?filter=eq:id:1?filter=(eq:id:1 & like:name:"test") | gt:created:"2020-01-01 00:00:00"sort parameter.<field>:[asc,desc].?sort=id:asc?sort=created:descHow can I help you explore Laravel packages today?