Giter Site home page Giter Site logo

aratinau / api-platform-pagination Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 4.0 1.2 MB

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

Shell 2.67% PHP 91.32% Twig 0.67% Makefile 2.67% Dockerfile 2.67%
php symfony api-platform

api-platform-pagination's Introduction

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

api-platform-pagination's People

Contributors

aratinau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

api-platform-pagination's Issues

new example: data provider + custom query with groupby and SUM + doctrine extensions

It will be great this example!

We are tring to use:

  • build a custom api with annotation for a specific entity,
  • data provider,
  • dtos (input and output),
  • data transformer for output (is it required?)
  • doctrine extensions for paginate, filter and order results

Our goal is build a query with group by clause and the result is with some SUM(s) ..

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.