rest-bundle
rest-bundle copied to clipboard
Example for Pagination
A very (very) common task for a list
-Endpoint (an endpoint that returns a collection) is pagination.
While it's possible and recommended (by me) to use Link
-Headers for pagination (and then smth like https://www.npmjs.com/package/parse-link-header) it is very common to add meta information to the request-body itself.
Example stolen from stackoverflow https://stackoverflow.com/questions/12168624/pagination-response-payload-from-a-restful-api
```json { "_metadata": { "page": 5, "per_page": 20, "page_count": 20, "total_count": 521, "Links": [ {"self": "/products?page=5&per_page=20"}, {"first": "/products?page=0&per_page=20"}, {"previous": "/products?page=4&per_page=20"}, {"next": "/products?page=6&per_page=20"}, {"last": "/products?page=26&per_page=20"}, ] }, "records": [ { "id": 1, "name": "Widget #1", "uri": "/products/1" }, { "id": 2, "name": "Widget #2", "uri": "/products/2" }, { "id": 3, "name": "Widget #3", "uri": "/products/3" } ] } ```I think it's at least worth an example to do so with symfony/serializer
since you'll have to create a PaginatedResponse
or something which is serializeable by symfony/serializer
.
I'm not sure if the bundle does already anything for pagination except that you can get an object populated from the query_string (instead of request body).
So it would be a question, what the bundle can do to make implementing pagination as easy as possible without setting any "self-made" convention.
Do you know, if there is an HTTP-standard for handling pagination? Because if, i would love to think of a solution. If not, I would maybe leave this up to the user.
If you have an idea how to implement this already with some easy steps, feel free to send a PR to https://github.com/violines/rest-bundle-showcase
You can even change some functionality there if you want. The whole purpose is that the features are shown, so the functionality is absolutely secondary.
I'm not sure if the bundle does already anything for pagination
It doesn't, but it's possible by creating an (serializable) object, which holds the information and the collection for the current page.
I think there should be at least an example, since close to every API I know has some sort of pagination somewhere.
Do you know, if there is an HTTP-standard for handling pagination? Because if, i would love to think of a solution. There isn't (as far as I know), but one can use the
Link
Header for it: https://tools.ietf.org/html/rfc5988#page-6
Pagination in general is opinionated, and could depend on a QueryBuilder or something - so I don't think it needs to be in you bundle.
But an example in the Readme could help people getting started :-) (I could help with providing possible solutions. but maybe you have already something in place in your use cases?)
the read from persistance layer will definitely be outside of the scope.
I think I should first implement a flexible solution for https://github.com/violines/rest-bundle-showcase and see if it makes sense to add it to the docs. I generally like the idea, because i definitely agree that this is a very common use case.
I'll add an example to the showcase repo when I have some time.
Hey @HellPat , i started working on some parts that will play into that (the internals for filtering, paginate, on db level). So if you plan to do anything here, maybe drop a quick message to avoid conflicting/duplicate work ;) best, Simon
Hey Simon, I'm not it at the moment, so there will be no conflicting work :-)
Hey, maybe some inspiration: https://tools.ietf.org/html/rfc5988
5.5. Examples
For example:
Link: <http://example.com/TheBook/chapter2>; rel="previous";
title="previous chapter"
indicates that "chapter2" is previous to this resource in a logical
navigation path.
Similarly,
Link: </>; rel="http://example.net/foo"
indicates that the root resource ("/") is related to this resource
with the extension relation type "http://example.net/foo".