GETting a Collection


Now we know how to GET a single record from our Symfony REST API, the next logical step is to return multiple records. That's where our Collection resource comes in to play.

Conceptually this isn't hugely different from returning a single record. We want to call one of our API end-points (though this would be a different from the single record end-point) and then return an array of results.

Because we have followed a sensible structure to our project, implementing this feature is straightforward. Which is nice.

We're going with our REST API testing approach and begin by writing our new tests to cover the end point. From there we can go ahead and implement the feature with confidence.

Then we will add in a way to limit the amount of results that are returned, and make a start on giving our API consumer a way to paginate the result set. We do this using a $limit and $offset. We're going to use the FOS REST Bundle QueryParam annotations for this. This has a little gotcha' associated with it that I routinely forget, so we cover the error message you will see and then the quick fix for how to rectify it.

Lastly we cover off how to set up some sensible defaults for our $limit and $offset parameters.

And with that we are well on our way to a well tested Symfony REST API.

Code For This Course

Get the code for this course.

Code For This Video

Get the code for this video.

Episodes