The Serializer
Google for Symfony serializer and find a page called
The Serializer Component.
API Platform is built on top of the Symfony components. And the entire process
of how it turns our CheeseListing object into JSON... and JSON back into a
CheeseListing object, is done by…
Custom Paginator
…directory, create a new PHP class called
DailyStatsPaginator. The only rule is that this needs to implement a
PaginatorInterface from ApiPlatform:
I'll go to "Code"->"Generate" - or Command+N on a Mac - and select
"Implement Methods". Select all five methods we need.
Perfect…
Base Test Class full of Goodies
…See this headers key? We
can remove that... and we have one more in CustomApiTestCase that we can
also remove.
But wait... didn't we need this so that API Platform knows we're sending data
in the right format? Absolutely. But... when you pass…
Filtering Related Collections
…solution: instead of
embedding these two properties... and potentially exposing the data of an
unpublished CheeseListing, you could configure API Platform to only return the
IRI string.
As a reminder, each item under cheeseListings contains two fields: title and
price. Why only those two fields…
Custom Resource State Provider
…coded new DailyQuest()
objects. They're both empty... because that class doesn't have any properties.
To tell API Platform to use the shiny new provider, in DailyQuest,
add provider set to DailyQuestStateProvider::class.
Let's give this a whirl! Dash back over to the…
stateOptions + entityClass Magic
…we have this stateOptions + entityClass thing, API Platform
sets the provider and the processor automatically to the core Doctrine ones.
So we don't even need to have the provider key: it's set for us.
Okay, but if the provider is querying for User…
Validation
…most importantly, every error has the same structure.
The status code is also 400 - which means the client made an error in the request -
and hydra:description says "Syntax error". Without doing anything, API Platform
is already handling this case. Oh, and the trace, while…
JSON-LD: Context for your Data
…you're not wrong. But more on that in a little while.
Anyways, the really cool thing is that API Platform is getting all of the data
about our class and its properties from our code! For example, look at the
CheeseListing/price property: it…
Context Builder & Service Decoration
When you make a GET request for a collection of users or a single user, API Platform
will use the same normalization group: user:read. This means the response will
always contain the email, username and cheeseListings fields.
Now we need to do something smarter…
Pagination & Foundry Fixtures
…configuration... it doesn't necessarily
show us all possible keys.
To see that, instead of debug:config, run:
php bin/console config:dump api_platform
debug:config shows you the current configuration. config:dump shows you a full
tree of possible configuration. Now... we see…
Deny Access with The "security" Option
…a role to use anything in your API by targeting URLs starting with /api.
In a traditional web app, I do use access_control for several things. But most
of the time I put my authorization rules inside controllers. But... of course,
with API Platform…
Validation Groups & Patch Formats
…greater interwebs, there are competing formats
for how the data should look when using a PATCH request and each format means
something different.
Currently, API Platform supports only one of these formats: application/merge-patch+json.
This format is... kind of what you expect. It…
Logging in Inside the Test
We've added two options when we make the request: a Content-Type header, to tell
API Platform that any data we send will be JSON-formatted, and a json option set
to an empty array which is enough to at least send some valid…
Decorating Data Persisters vs Context Builders
…called just DataPersister. Back in PhpStorm, hit
Shift+Shift, search for DataPersister and make sure to include "non-project items".
Select the one from ApiPlatform's Doctrine Bridge.
And... cool! This looks pretty much exactly like we expected: it persists and
flushes. This is what…
Auto-set the Owner: Entity Listener
…and
move @ORM\Entity to the bottom... so it's not mixed up in the middle of all the
API Platform stuff. Now add @ORM\EntityListeners() and pass this an array with
one item inside: the full class name of the entity listener class:
App…
Embedded Relations
…but more importantly the normalization
context... including groups set to the two we expect.
This is also cool because you can see other context options that are set by
API Platform. These control certain internal behavior.
Next: let's get crazy with our relationships by…
Relations & Iris
…s database id. And we found out that API Platform did not like that.
It said: "expected IRI". But what is an IRI?
We mentioned this term once earlier in the tutorial. Go back down to the
GET /api/users collection endpoint. We know that…
Validation
…API platform creates a DragonTreasure object...
but doesn't set any data on it. And then it explodes when it hits the database because
some of the columns are null.
And, we expected this! We're missing validation. Adding validation to our API
is exactly…
Totally Custom Resource
…on an
entity, API Platform automatically gives you processors and providers. When you
create a custom class, you start with no providers and no processors. This
means that API Platform has no idea how to load data when you make a GET
request... nor how…
Filtering Relation Collection
…for those treasures
and the query collection extension is called.
But when we make a call to a user endpoint - like to GET a single User -
API Platform is not making a query for any treasures: it's making a query for
that one User…
x
1000+