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…
Quick! Create a DragonTreasure DTO
…be it! We
now have a good old-fashioned, boring entity class. In src/ApiPlatform/,
let's also delete AdminGroupsContextBuilder. This was a complex way to make
fields readable or writable by our admin... but we're going to solve that with
ApiProperty security. Also…
Writable Relation Fields
…those fields from inside our data mapper. Then API Platform handles transforming
them into IRIs.
One thing we haven't talked about is being able to write to one of these
relation fields.
When we use this post() endpoint, we don't need to send…
DTO Input "Initializer"
With an input DTO, the update process works like this. First ApiPlatform queries
for the CheeseListing entity. Second, the JSON is deserialized into a new
CheeseListingInput object. And third, our transform() method is called,
where we take that CheeseListingInput object's data and move it…
DTO Data Transformer
…actually work, we need to write
code that converts our CheeseListing object into a CheeseListingOutput object
so that API Platform can serialize it. The "thing" that does that is called a data
transformer.
Let's create one in the src/ directory: add a new directory…
Why/When a Many Relation is IRI Strings vs Embedded
…In DailyStats, to serialize - or "normalize" - we're using a group called
daily-stats:read:
Whenever API Platform sees an embedded object:
It looks at that object - so CheeseListing - and looks to see if any of
its fields are in that same group. And there…
Doctrine postLoad Listener
…I know, we're getting crazy, but
I want to keep seeing how far we can push API Platform's abilities.
This new field will be similar to isMe... but just different enough
that it will lead us to an alternate solution. Suppose some users…
Completely Custom Field via a Data Provider
…providers that
load one object for the item operations.
Normally, we don't need to think about this system because API Platform has a
built-in Doctrine data provider that handles all of it for us. But if we want to
load some extra data…
App & Test Setup
…things that we did in the last course was create a custom data
persister: UserDataPersister:
Let's see why we did this, use decoration to make it more powerful and learn
how data persisters are different than some other parts of ApiPlatform, like
context builders.
Relations and IRIs
…user in the database. But... it didn't like it! Why? Because in
API Platform and, commonly, in modern API development in general, we do not use
ids to refer to resources: we use IRIs. For me, this was strange at first... but
I quickly…
Filters: Searching Results
…down to $isPublished. Paste
this above. And now, we don't need the properties option anymore... API Platform
figures that out on its own:
The result? The same as before. I won't try it, but if you peek at the collection
endpoint, it still…
Access Token Authenticator
…API how
users are able to authenticate. Fortunately we can do this via API Platform.
Open up config/packages/api_platform.yaml. And a new key called swagger,
though we're actually configuring the OpenAPI docs. To add a new way of
authenticating, set api…
Testing Token Authentication
…scopes, and then it would create that token and set
it into the header
This totally does not work right now, but check out Browser's docs to learn how.
Next: in API Platform 3.1, the behavior of the PUT operation is changing. Let…
Entity -> DTO Item State Provider
…the endpoint works beautifully. If we try an
invalid id, our provider returns null and API Platform takes care of the 404.
Side note: if you follow some of these related treasures, they may 404 as well.
Let's see... we have 21 and 27…
Pagination
…s because API Platform shows 30 results per page, by default. Because I
don't feel like adding 20 more manually, this is a great time to learn how
to change that!
First, this can be changed globally in your config/packages/api_platform.yaml…
DTO Quirks: Embedded Objects
…on a related object will be serialized, then
API Platform should return an array of IRI strings instead of embedding the
objects.
This is a bug in how the readableLink for properties is calculated when you have
a DTO. I've actually fixed this bug…
Input DTO Class
…needed anymore
because we have moved all of this stuff. Delete both:
There is now nothing inside of CheeseListing about serializing or deserializing.
Ok! Our CheeseListingInput is ready! To tell API Platform to use it, it's
the same as output. On CheeseListing, add input=…
Custom Filter for Custom Resources
…go to
/api/daily-stats.jsonld, we see all the stats. But if we add ?from=2020-09-01,
we want to only show stats on that day or later.
Let's do this! Start basically the same way as before: in the
src/ApiPlatform/ directory…
Completely Custom Resource
…types, which will also
help API Platform's documentation. More on that soon. Or, if you don't want
this stuff to be public, use the normal private properties with getters and setters:
whatever you want.
To officially make this an API Resource class, above…
Setting a Custom Field Via a Listener
…We just set the $isMe field on the authenticated User object. One cool
thing about Doctrine is that if API platform later queries for that same user,
Doctrine will return this exact object in memory, which means that the
$isMe field will be set to…
x
1000+