{"id":13796182,"url":"https://github.com/aratinau/api-platform-pagination","last_synced_at":"2025-05-13T00:30:43.372Z","repository":{"id":40466760,"uuid":"382312743","full_name":"aratinau/api-platform-pagination","owner":"aratinau","description":"Examples using Api-Platform on custom data with filter, sort and pagination using DataProvider","archived":false,"fork":false,"pushed_at":"2022-05-06T13:52:51.000Z","size":1226,"stargazers_count":14,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-03T23:06:28.569Z","etag":null,"topics":["api-platform","php","symfony"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aratinau.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-02T10:34:52.000Z","updated_at":"2024-05-11T23:09:05.000Z","dependencies_parsed_at":"2022-08-09T21:20:26.197Z","dependency_job_id":null,"html_url":"https://github.com/aratinau/api-platform-pagination","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aratinau%2Fapi-platform-pagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aratinau%2Fapi-platform-pagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aratinau%2Fapi-platform-pagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aratinau%2Fapi-platform-pagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aratinau","download_url":"https://codeload.github.com/aratinau/api-platform-pagination/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225159846,"owners_count":17430191,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-platform","php","symfony"],"created_at":"2024-08-03T23:01:07.284Z","updated_at":"2024-11-18T10:31:05.955Z","avatar_url":"https://github.com/aratinau.png","language":"PHP","funding_links":[],"categories":["Table of Contents"],"sub_categories":["awesome-api-platform-bundles"],"readme":"# Api Platform Sort, Filter and Pagination on custom data\n\nInspired by https://github.com/api-platform/demo\n\n# Summary\n\n- [First Example use raw data from a csv file](#first-example-use-raw-data-from-a-csv-file)\n- [Second example use custom controller](#second-example-use-custom-controller)\n- [Third example use MovieCollectionDataProvider and repository](#third-example-use-moviecollectiondataprovider-and-repository)\n- [Fourth example use QueryBuilder in CarCollectionDataProvider (collectionExtensions)](#fourth-example-use-querybuilder-in-carcollectiondataprovider-collectionextensions)\n- [Fifth example use JobCollectionDataProvider (paginationExtension)](#fifth-example-use-jobcollectiondataprovider-paginationextension)\n- [Sixth example use FurnitureDataProvider (collectionDataProvider)](#sixth-example-use-furnituredataprovider-collectiondataprovider)\n- [Seventh example use QueryBuilder in subresource](#seventh-example--simple-dataprovider-using-subresourcedataprovider)\n- [Eighth example use QueryBuilder in subresource](#eighth-example-use-querybuilder-in-subresource)\n- [Ninth use custom subresource with provider (without subresourceDataProvider)](#ninth-example---custom-subresource-with-provider-without-subresourcedataprovider)\n- [Tenth example - Custom Paginator in Provider with QueryBuilder](#tenth-example---custom-paginator-in-provider-with-querybuilder)\n- [🚀How to create a Collection Data Provider and keep Doctrine Extension, Filters and Pagination on it](#how-to-create-a-collection-data-provider-and-keep-doctrine-extension-filters-and-pagination-on-it)\n\n## Install\n\nBuild the project `make build`\n\nStart the project `make start`\n\nEnter the php container with `make sh`\n\nInstall dependencies (in php container) with `composer install`\n\nLoad fixtures with `make init-fixtures`\n\nYou can go to `http://127.0.0.1:8000/api/`\n\n\n## First Example use raw data from a csv file\n\nExample using a custom `CityFilter` filtering on raw data from the file `Repository/City/data/worldcities.csv`\n\nCityFilter pass the `sort` and `order` params to the context and this context it's processed in the `CityCollectionDataProvider`\n\nThe pagination is available\n\n### Usage\n\nsort and filter on raw data from csv\n`/api/cities?search[key]=tokyo\u0026order[key]=desc\u0026page=1`\n\n#### Keys available\n\n- id\n- city\n- city_ascii\n- lat\n- lng\n- country\n- iso2\n- iso3\n- admin_name\n- capital\n- population\n\n###### Commits related\n\n- https://github.com/aratinau/api-platform-pagination/commit/f40232ed62d2720fa3140f6ce6cc89c36798dd6a\n- https://github.com/aratinau/api-platform-pagination/commit/1d34f49c1520040a88c63b3f1d391ad3d24e8248\n\n\n## Second example use custom controller\n\nOn path `/movies/custom-action` the action `CustomMovieAction` is called and page and order use `getCustom` method from `MovieRepository`.\nThe pagination is available\n\n### Usage\n\ncustom controller action from database using Paginator\n`api/movies/custom-action?page=1\u0026order[id]=asc\u0026order[title]=desc`\n\n###### Commits related\n\n- https://github.com/aratinau/api-platform-pagination/commit/ea7db84cb60b1a6ad141894849e8f9ad5b982e84\n\n## Third example use MovieCollectionDataProvider and repository\n\nOn 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.\nThe param `order` can be `title` or `id` and ordered by `asc|desc`. The pagination is available\n\n### Usage\n\ndata provider using repository (by group from normalization_context on Movie entity)\n`api/movies/custom-action-using-dataprovider?page=2\u0026order[title]=desc\u0026isPublished=false`\n\n###### Commits related\n\n- https://github.com/aratinau/api-platform-pagination/commit/c33a7940a367152b9457607e11c5e65970aceb9b\n- https://github.com/aratinau/api-platform-pagination/commit/861cfc9267958825640ff549befb29f386b0a252\n\n## Fourth example use QueryBuilder in CarCollectionDataProvider (collectionExtensions)\n\nThis example show how use QueryBuilder and filters with CollectionExtensions in CarCollectionDataProvider.\nThe collection is filtered by color from the `context` with the QueryBuilder and filtered by `name` and `isPublished` from SearchFilter in Car entity.\nThe pagination is available\n\n### Usage\n\n`/api/cars?color=color_name\u0026isPublished=true|false\u0026page=1`\n\n#### Color name available\n\n- red\n- orange\n- green\n- yellow\n- black\n\n###### Commits related\n\n- https://github.com/aratinau/api-platform-pagination/commit/4d8d22a4ccad265b7de1bffc15729df06eb5ded4\n\n## Fifth example use JobCollectionDataProvider (paginationExtension)\n\nThis example show how use PaginationExtension in JobCollectionDataProvider\n\n### Usage\n\n`api/jobs?page=1`\n\n###### Commits related\n\n- https://github.com/aratinau/api-platform-pagination/commit/53ce8298d7c28e17fc0a55c5c78ecb0ce2fc9a1d\n\n## Sixth example use FurnitureDataProvider (collectionDataProvider)\n\nBasic example showing how use and configure CollectionDataProvider\n\n### Usage\n\n`api/furniture?page=2`\n\n###### Commits related\n\n- https://github.com/aratinau/api-platform-pagination/commit/2cab86c65dd40ba218bca175f217581e94660b7b\n\n## Seventh example : simple DataProvider using subresourceDataProvider\n\nCommentSubresourceDataProvider show how use the standard behaviour\n\n### Usage\n\n`api/movies/{id}/comments`\n\n###### Commits related\n\n- https://github.com/aratinau/api-platform-pagination/commit/6addf42683e8278b8f81afadc11d269a81a17f9e\n\n## Eighth example use QueryBuilder in subresource\n\nDiscussionSubresourceDataProvider show how use QueryBuilder and order by DESC the result\n\n### Usage\n\n`api/discussions/{id}/messages?page=1`\n\n###### Commits related\n\n- https://github.com/aratinau/api-platform-pagination/commit/29ac71e2f98024439a8cffc81287a41ec0afe4a3\n\n## Ninth example - custom subresource with provider (without subresourceDataProvider)\n\nWith JobDataProvider and path `jobs/{id}/employees` return employees from id's job\n\n[ ] TODO Pagination\n\n### Usage\n\n`api/jobs/{id}/employees/{arg1}`\n\n###### Commits related\n\n- https://github.com/aratinau/api-platform-pagination/commit/5be041c859ce58e0ccc894fe1efd0ed7855b6dc4\n\n## Tenth example - Custom Paginator in Provider with QueryBuilder\n\nPostCollectionDataProvider call the method findLatest from PostRepository and PostRepository call PostPaginator\n\n### Usage\n\n`/api/posts?page=2`\n\n###### Commits related\n\n- https://github.com/aratinau/api-platform-pagination/commit/9a0989e3cab1ef7faceeabc3217098a54c75906d\n\n## Notes\n\n`Album` and `Artist` are ready to be used\n\n## How to create a Collection Data Provider and keep Doctrine Extension, Filters and Pagination on it\n\nWe're going to create a Provider who will return only books not archived when the param `includeArchived` is missing. \nIt will return the books by the locale corresponding in a Doctrine Extension\n\n### Usage\n`/api/books` or `/api/books?includeArchived=false` return only books not archived\n\n`/api/books?includeArchived=true` return all books (archived or not)\n\n### Quick explanation\n\nIn our `BookCollectionDataProvider` we have this condition:\n\n```php\n$includeArchived = $context['filters']['includeArchived'] ?? 'false';\n// ...\nif ($includeArchived === 'false') {\n    $queryBuilder-\u003eandWhere(\"$alias.isArchived = false\");\n}\n```\n\nThen we create a `BookExtension` and add this condition to return book by locale defined.\n\n```php\n-\u003eandWhere(\"$rootAlias.locale = :locale\")\n-\u003esetParameter('locale', self::LOCALE)\n```\n\nThen in `BookCollectionDataProvider` we can loop over all the extensions on `$collectionExtensions` (we defined $collectionExtensions on `services.yaml`) so it apply to all extensions.\n\nExtensions called are (in this order): \n\n- `\"App\\Doctrine\\BookExtension\"`\n- `\"ApiPlatform\\Doctrine\\Orm\\Extension\\FilterExtension\"`\n- `\"ApiPlatform\\Doctrine\\Orm\\Extension\\FilterEagerLoadingExtension\"`\n- `\"ApiPlatform\\Doctrine\\Orm\\Extension\\EagerLoadingExtension\"`\n- `\"ApiPlatform\\Doctrine\\Orm\\Extension\\OrderExtension\"`\n- `\"ApiPlatform\\Doctrine\\Orm\\Extension\\PaginationExtension\"`\n\nNow 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.\n\n###### Commits related\n\n- https://github.com/aratinau/api-platform-pagination/commit/1d27f16adecda1c0956fcc5d0da81f017d915c1b\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faratinau%2Fapi-platform-pagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faratinau%2Fapi-platform-pagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faratinau%2Fapi-platform-pagination/lists"}