Api Tests & Assertions
…401 status code. Let's assert that!
$this->assertResponseStatusCodeSame(401).
That assertion is not part of PHPUnit: we get that - and a bunch of other nice
assertions - from API Platform's test classes.
Let's try this! Run the test:
php bin/phpunit
Oh, interesting…
Creating JSON API Endpoints
…an API, you can absolutely do that with Symfony. In fact,
it's a fantastic option in part, because of API Platform. That's a framework
for creating APIs built on top of Symfony that both makes building your API fast
and creates an API…
API Tokens? Session Cookies?
…adds complexity to your setup, it also hurts performance. Kévin
Dunglas - the lead developer of API Platform - has a
blog post
about this. He even shows a strategy where your frontend and backend can live
in totally different directories or repositories, but still live on…
API Debugging with the Profiler
…profiler
to see a list of the most recent request. Here's the one for /api/cheese_listings.
Click the token to jump into its profiler.
Oh, and API Platform adds its own profiler panel, which is a nice way to see
which resource this…
User API Resource
…we're crushing it!
And now we get to the really interesting part. We need to "relate" our two
resources so that each treasure is owned by a user. What does that look like in
API Platform? It's super interesting, and it's next.
User API & the Serializer
…other words, when we refresh the endpoint,
it works!
We're not going to talk too much about how the Symfony serializer works - we talk
a lot about it in our API Platform tutorials. But let's at least get some basics.
By default, the…
Swagger: Instant, Interactive API Docs
…but with
special keys - like @context - that have a specific meaning. Understanding
JSON-LD is an important part of leveraging API Platform - and we'll talk more about
it soon.
Anyways, to make things more interesting - go back to the POST endpoint and
create a…
User API Resource
…want you to notice is that, so far, the primary key is always being
used as the "id" in our API. This is something that's flexible in API Platform.
In fact, instead of using an auto-increment id, one option is to use a…
Cart API & Data
…you're an API Platform geek and want to see how that was implemented.
In a real app, I would store carts in the database.
So from a JavaScript perspective, the important thing is that we have a traditional
set of API endpoints for our…
Hydra: Describing API Classes, Operations & More
…the same API.
Why do we have both? Hydra is a bit more powerful: there are certain things it can
describe that OpenAPI can't. But OpenAPI is a lot more common and has more tools
built on top of it. API platform provides both…
Logout & Passing API Data to JavaScript
…groups are configured in API Platform. So how did the serializer
know to use those?
The answer to that, as best I can tell, is that it's working... partially by accident.
During serialization, API Platform sees that we're serializing an "API resource"
and…
JSON API Endpoint
…a JsonResponse!
If you're building a serious API, Symfony has a
serializer component that's really good at turning objects into JSON... and then
JSON back into objects. We talk a lot about it in our API Platform tutorial, which
is a powerful library…
JSON API Endpoint
…an array, but it means that you could start passing objects to $this->json().
If you want to learn more - or want to build a super-rich API - check out our
tutorial about
API Platform: an amazing Symfony
bundle for building APIs.
Next, let's…
Hydra: Describing API Classes, Operations & More
…in some cases, having an OpenAPI specification will be useful - like
to use Swagger - and other times, the JSON-LD Hydra document will be useful. With
API Platform, you get both.
Phew! Ok, enough theory! Let's get back to building and customizing our API.
UUID as a API Identifier
…do we tell API Platform to use the uuid property as the "identifier"?
It's actually pretty simple! And we talked about it before.
Go to the top of User. Every API Resource needs an identifier. And when you
use Doctrine, API Platform assumes that…
Operations / Endpoints
…That...
sounds like two ways of saying the same thing!
The topic of PUT versus PATCH in APIs can get spicy. But in API Platform, at
least today, PUT and PATCH work the same: they're both used to update a resource.
And we'll…
More Formats: HAL & CSV
…json. Setting
this header is easy to do in JavaScript, and if you don't set it,
JSON-LD is the default format.
API Platform uses three formats out of the box. You can see them down here on the
bottom of the docs page…
State Providers, Processors & a Custom Field
…re going to dive even
deeper in this tutorial.
Nestled within the "Upgrade Guide" of API Platform's docs lives one of my favorite sections
on this very topic. Each API resource class - whether it's an entity
or a normal class - will have a…
More Formats: HAL & CSV
…one: jsonhal.
This is one of the other formats that API Platform supports out-of-the box. Below, add
mime_types: then the standard content type for this format: application/hal+json.
Cool! And just like that... our entire API supports a new format! Refresh…
The Serializer
The key behind how API platform turns our objects into JSON... and also how it
transforms JSON back into objects is Symfony's Serializer. symfony/serializer
is a standalone component that you can use outside of API platform and it's
awesome. You give it…
x
1000+