Login to bookmark this video
Buy Access to Course
31.

Output DTO Class

Share this awesome video!

|

Keep on Learning!

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

So far, every ApiResource class is either an entity - like CheeseListing and User - or a completely custom class, like DailyStats. With an entity, we can add complex custom fields with some work. We did this in User with our custom isMe and isMvp fields:

290 lines | src/Entity/User.php
// ... lines 1 - 42
class User implements UserInterface
{
// ... lines 45 - 98
/**
* Returns true if this is the currently-authenticated user
*
* @Groups({"user:read"})
*/
private $isMe = false;
/**
* Returns true if this user is an MVP
*
* @Groups({"user:read"})
*/
private $isMvp = false;
// ... lines 112 - 288
}

And of course, with a custom class like DailyStats that is not an entity... we can do whatever we want! We get to make these classes look exactly like our API. On the downside, these take more time to set up and we lose automatic features like pagination and filtering.

But, like Jean-Luc Picard searching for a solution to an impossible situation, there is a third option, which is kind of "in between" these two.

Why DTO Classes?

In CheeseListing, the input fields look quite different from the output fields. For example, the isPublished field is writable, but it's not readable:

221 lines | src/Entity/CheeseListing.php
// ... lines 1 - 60
class CheeseListing
{
// ... lines 63 - 102
/**
* @ORM\Column(type="boolean")
* @Groups({"cheese:write"})
*/
private $isPublished = false;
// ... lines 108 - 219
}

And the description property is readable, but not writable:

221 lines | src/Entity/CheeseListing.php
// ... lines 1 - 60
class CheeseListing
{
// ... lines 63 - 81
/**
* @ORM\Column(type="text")
* @Groups({"cheese:read"})
* @Assert\NotBlank()
*/
private $description;
// ... lines 88 - 219
}

Well, it is writable, but via a different way: a setTextDescription() method:

221 lines | src/Entity/CheeseListing.php
// ... lines 1 - 60
class CheeseListing
{
// ... lines 63 - 156
/**
* The description of the cheese as raw text.
*
* @Groups({"cheese:write", "user:write"})
* @SerializedName("description")
*/
public function setTextDescription(string $description): self
{
$this->description = nl2br($description);
return $this;
}
// ... lines 169 - 219
}

We accomplished all of this by using smart serialization groups and some custom methods. The upside is that... it's simple! All the logic is in one class. The downside is that... well... it's not actually that simple. Our serialization and deserialization rules are not super clear: you can't quickly look at CheeseListing and figure out which fields are going to be readable or writable.

One solution to this is to have a separate class for your input and output. Basically, we would transform a CheeseListing into another object and then that object would be serialized and returned to the User. We can also do the same thing for the input, which we'll talk about later.

This feature is called input and output DTO's, for data transfer objects. And I love this approach... in theory. Implementing it is pretty clean and it gives you a lot of flexibility. But it's also not a feature that is heavily used by the core API Platform devs. And I found some quirks... some of which are already fixed. I'll walk you through them along the way.

Creating the Output Class

So how do we start? By creating a class that has the exact fields that should exist when a CheeseListing is serialized. In src/, create a new directory called Dto/ and inside, a new PHP class called CheeseListingOutput:

9 lines | src/Dto/CheeseListingOutput.php
// ... lines 1 - 2
namespace App\Dto;
class CheeseListingOutput
{
// ... line 7
}

For now let's just have a public $title property:

9 lines | src/Dto/CheeseListingOutput.php
// ... lines 1 - 4
class CheeseListingOutput
{
public $title;
}

I'm going to use public properties for a few reason. First, these classes should be simple and are only used for our API. And second, if you're using PHP 7.4, you can add types to the properties to guarantee they're set correctly.

Anyways, we'll add more properties later, but let's see if we can get this working.

To tell API Platform that we want to use this as the output class, we need to go back to CheeseListing and, inside the @ApiResource annotation - it doesn't matter where... but I like to put it on top - add output=, take off the quotes and say CheeseListingOutput::class. Go above to add this use statement manually: use CheeseListingOutput:

223 lines | src/Entity/CheeseListing.php
// ... lines 1 - 11
use App\Dto\CheeseListingOutput;
// ... lines 13 - 20
/**
* @ApiResource(
* output=CheeseListingOutput::CLASS,
// ... lines 24 - 47
* )
// ... lines 49 - 61
*/
class CheeseListing
{
// ... lines 65 - 221
}

Ok, before we do anything else, let's try it! Find your browser and open a new tab to /api/cheeses.jsonld. And... bah! Error because I always forget my comma. Let's try that again. This time... it... works?

Hmm... it's the exact same result as before. Why? Because right now API Platform is thinking:

Hey! You told me that you wanted to use CheeseListingOutput as your output representation. That's great! But... how do I create that object from a CheeseListing?

Yep! Something needs to transform our CheeseListing objects into CheeseListingOutput objects so that API platform can serialize them. What does that? A data transformer! Let's create one next.