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…
New PUT Behavior
…what does that mean? Right now, it means nothing has changed: our PUT
operation behaves like it always has. But, in API Platform 4, the behavior of PUT
will change dramatically. And, at some point between now and then, we need to
opt into that…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
Data Persister Decoration
…create this UserDataPersister class:
Now, let's back up real quick. Whenever you use a POST or PUT endpoint, after
ApiPlatform deserializes the data into an object and validates it, it tries
to save or persist that object. Usually, this means that we're saving…
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…
x
1000+