Skip to content

Examples using Api-Platform on custom data with filter, sort and pagination using DataProvider

Notifications You must be signed in to change notification settings

aratinau/api-platform-pagination

Repository files navigation

Api Platform Sort, Filter and Pagination on custom data

Inspired by https://github.com/api-platform/demo

Summary

Install

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/

First Example use raw data from a csv file

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

Usage

sort and filter on raw data from csv /api/cities?search[key]=tokyo&order[key]=desc&page=1

Keys available

  • id
  • city
  • city_ascii
  • lat
  • lng
  • country
  • iso2
  • iso3
  • admin_name
  • capital
  • population
Commits related

Second example use custom controller

On path /movies/custom-action the action CustomMovieAction is called and page and order use getCustom method from MovieRepository. The pagination is available

Usage

custom controller action from database using Paginator api/movies/custom-action?page=1&order[id]=asc&order[title]=desc

Commits related

Third example use MovieCollectionDataProvider and repository

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

Usage

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

Commits related

Fourth example use QueryBuilder in CarCollectionDataProvider (collectionExtensions)

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

Usage

/api/cars?color=color_name&isPublished=true|false&page=1

Color name available

  • red
  • orange
  • green
  • yellow
  • black
Commits related

Fifth example use JobCollectionDataProvider (paginationExtension)

This example show how use PaginationExtension in JobCollectionDataProvider

Usage

api/jobs?page=1

Commits related

Sixth example use FurnitureDataProvider (collectionDataProvider)

Basic example showing how use and configure CollectionDataProvider

Usage

api/furniture?page=2

Commits related

Seventh example : simple DataProvider using subresourceDataProvider

CommentSubresourceDataProvider show how use the standard behaviour

Usage

api/movies/{id}/comments

Commits related

Eighth example use QueryBuilder in subresource

DiscussionSubresourceDataProvider show how use QueryBuilder and order by DESC the result

Usage

api/discussions/{id}/messages?page=1

Commits related

Ninth example - custom subresource with provider (without subresourceDataProvider)

With JobDataProvider and path jobs/{id}/employees return employees from id's job

[ ] TODO Pagination

Usage

api/jobs/{id}/employees/{arg1}

Commits related

Tenth example - Custom Paginator in Provider with QueryBuilder

PostCollectionDataProvider call the method findLatest from PostRepository and PostRepository call PostPaginator

Usage

/api/posts?page=2

Commits related

Notes

Album and Artist are ready to be used

How to create a Collection Data Provider and keep Doctrine Extension, Filters and Pagination on it

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

Usage

/api/books or /api/books?includeArchived=false return only books not archived

/api/books?includeArchived=true return all books (archived or not)

Quick explanation

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.

Commits related

About

Examples using Api-Platform on custom data with filter, sort and pagination using DataProvider

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published