Inspired by https://github.com/api-platform/demo
- First Example use raw data from a csv file
- Second example use custom controller
- Third example use MovieCollectionDataProvider and repository
- Fourth example use QueryBuilder in CarCollectionDataProvider (collectionExtensions)
- Fifth example use JobCollectionDataProvider (paginationExtension)
- Sixth example use FurnitureDataProvider (collectionDataProvider)
- Seventh example use QueryBuilder in subresource
- Eighth example use QueryBuilder in subresource
- Ninth use custom subresource with provider (without subresourceDataProvider)
- Tenth example - Custom Paginator in Provider with QueryBuilder
- 🚀How to create a Collection Data Provider and keep Doctrine Extension, Filters and Pagination on it
Build the project make build
Start the project make start
Enter the php container with make sh
Install dependencies (in php container) with composer install
Load fixtures with make init-fixtures
You can go to http://127.0.0.1:8000/api/
Example using a custom CityFilter
filtering on raw data from the file Repository/City/data/worldcities.csv
CityFilter pass the sort
and order
params to the context and this context it's processed in the CityCollectionDataProvider
The pagination is available
sort and filter on raw data from csv
/api/cities?search[key]=tokyo&order[key]=desc&page=1
- id
- city
- city_ascii
- lat
- lng
- country
- iso2
- iso3
- admin_name
- capital
- population
- https://github.com/aratinau/api-platform-pagination/commit/f40232ed62d2720fa3140f6ce6cc89c36798dd6a
- https://github.com/aratinau/api-platform-pagination/commit/1d34f49c1520040a88c63b3f1d391ad3d24e8248
On path /movies/custom-action
the action CustomMovieAction
is called and page and order use getCustom
method from MovieRepository
.
The pagination is available
custom controller action from database using Paginator
api/movies/custom-action?page=1&order[id]=asc&order[title]=desc
On the normalization_context group normalization-custom-action-using-dataprovider
the MovieCollectionDataProvider is called and the resust is from the repository. The param isPublished=true|false
can be used and the result is filtered by the value asked.
The param order
can be title
or id
and ordered by asc|desc
. The pagination is available
data provider using repository (by group from normalization_context on Movie entity)
api/movies/custom-action-using-dataprovider?page=2&order[title]=desc&isPublished=false
- https://github.com/aratinau/api-platform-pagination/commit/c33a7940a367152b9457607e11c5e65970aceb9b
- https://github.com/aratinau/api-platform-pagination/commit/861cfc9267958825640ff549befb29f386b0a252
This example show how use QueryBuilder and filters with CollectionExtensions in CarCollectionDataProvider.
The collection is filtered by color from the context
with the QueryBuilder and filtered by name
and isPublished
from SearchFilter in Car entity.
The pagination is available
/api/cars?color=color_name&isPublished=true|false&page=1
- red
- orange
- green
- yellow
- black
This example show how use PaginationExtension in JobCollectionDataProvider
api/jobs?page=1
Basic example showing how use and configure CollectionDataProvider
api/furniture?page=2
CommentSubresourceDataProvider show how use the standard behaviour
api/movies/{id}/comments
DiscussionSubresourceDataProvider show how use QueryBuilder and order by DESC the result
api/discussions/{id}/messages?page=1
With JobDataProvider and path jobs/{id}/employees
return employees from id's job
[ ] TODO Pagination
api/jobs/{id}/employees/{arg1}
PostCollectionDataProvider call the method findLatest from PostRepository and PostRepository call PostPaginator
/api/posts?page=2
Album
and Artist
are ready to be used
We're going to create a Provider who will return only books not archived when the param includeArchived
is missing.
It will return the books by the locale corresponding in a Doctrine Extension
/api/books
or /api/books?includeArchived=false
return only books not archived
/api/books?includeArchived=true
return all books (archived or not)
In our BookCollectionDataProvider
we have this condition:
$includeArchived = $context['filters']['includeArchived'] ?? 'false';
// ...
if ($includeArchived === 'false') {
$queryBuilder->andWhere("$alias.isArchived = false");
}
Then we create a BookExtension
and add this condition to return book by locale defined.
->andWhere("$rootAlias.locale = :locale")
->setParameter('locale', self::LOCALE)
Then in BookCollectionDataProvider
we can loop over all the extensions on $collectionExtensions
(we defined $collectionExtensions on services.yaml
) so it apply to all extensions.
Extensions called are (in this order):
"App\Doctrine\BookExtension"
"ApiPlatform\Doctrine\Orm\Extension\FilterExtension"
"ApiPlatform\Doctrine\Orm\Extension\FilterEagerLoadingExtension"
"ApiPlatform\Doctrine\Orm\Extension\EagerLoadingExtension"
"ApiPlatform\Doctrine\Orm\Extension\OrderExtension"
"ApiPlatform\Doctrine\Orm\Extension\PaginationExtension"
Now you can still use the SortOrder
on the Book
entity for example, and add the param isArchived
and the result will be pass through the DoctrineExtension to set the locale.