GET'ting a collection of Albums [Raw Symfony 4]
We have covered how to GET
a single Album
by ID. We saw that we would need to have injected the AlbumRepository
, and that we could use the Symfony routing annotations to ensure we only got ID's that were numerical.
With that numerical value we could ask the repository for the Album
entity matching the given ID, and then return that as JSON, with a little detour through the jsonSerialize
method.
As it turned out, GET
ting just a single Album
wasn't that hard.
And Good News Everybody! Getting a collection of Albums is really straightforward. We've done all the hard work.
There's two things we need to do:
- Make sure we use a slightly different name for our route.
- Switch the
AlbumRepository
method call fromfind
tofindAll
.
Honestly, both of these are super straightforward - it's basically a case of copy / paste the existing get
method, and make some small adjustments:
/**
* @Route(
* "/album",
* name="cget_album",
* methods={"GET"}
* )
*
* @return JsonResponse
*/
public function cget()
{
return new JsonResponse(
$this->albumRepository->findAll(),
JsonResponse::HTTP_OK
);
}
I'm using the method name of cget
. 'c' here means Collection. Use any naming format you like. I've borrowed this from FOSRESTBundle.
Likewise, if you copy / paste from the existing get
method then make sure your @Route
name is updated. If you have two routes with the same name, Symfony will get confused and take the last route it finds with the given name. It won't tell you it's done this, either, which is frustrating.
Anyway, that's basically us done.
php vendor/bin/behat --tags=t
Feature: Provide a consistent standard JSON API endpoint
In order to build interchangeable front ends
As a JSON API developer
I need to allow Create, Read, Update, and Delete functionality
Background: # features/album.feature:7
Given there are Albums with the following details: # FeatureContext::thereAreAlbumsWithTheFollowingDetails()
| title | track_count | release_date |
| some fake album name | 12 | 2020-01-08T00:00:00+00:00 |
| another great album | 9 | 2019-01-07T23:22:21+00:00 |
| now that's what I call Album vol 2 | 23 | 2018-02-06T11:10:09+00:00 |
@t
Scenario: Can get a collection of Albums # features/album.feature:28
Given I request "/album" using HTTP GET # Imbo\BehatApiExtension\Context\ApiContext::requestPath()
Then the response code is 200 # Imbo\BehatApiExtension\Context\ApiContext::assertResponseCodeIs()
And the response body contains JSON: # Imbo\BehatApiExtension\Context\ApiContext::assertResponseBodyContainsJson()
"""
[
{
"id": 1,
"title": "some fake album name",
"track_count": 12,
"release_date": "2020-01-08T00:00:00+00:00"
},
{
"id": 2,
"title": "another great album",
"track_count": 9,
"release_date": "2019-01-07T23:22:21+00:00"
},
{
"id": 3,
"title": "now that's what I call Album vol 2",
"track_count": 23,
"release_date": "2018-02-06T11:10:09+00:00"
}
]
"""
1 scenario (1 passed)
4 steps (4 passed)
0m0.27s (9.71Mb)
Nice and easy, right?