Hello,
I am totally new in Symfony 4 & API Platform and I want to update my existing API (created with FOSRest).
I have lots of routes that return custom JSON objects, different from my entities.
For example, I have an Offer
entity
<?php // api/src/Entity/Offer.php namespace App\Entity; use ApiPlatform\Core\Annotation\ApiResource; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * An offer from my shop - this description will be automatically extracted form the PHPDoc to document the API. * * @ApiResource(iri="http://schema.org/Offer") * @ORM\Entity */ class Offer { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ public $id; /** * @ORM\Column(type="text") */ public $description; /** * @ORM\Column(type="float") * @Assert\NotBlank * @Assert\Range(min=0, minMessage="The price must be superior to 0.") * @Assert\Type(type="float") */ public $price; /** * @ORM\ManyToOne(targetEntity="Product", inversedBy="offers") */ public $product; }
And I want to return a JSON object based on that product entity but with custom fields.
Is it really mandatory to create a new resource just for that one GET method or is there a best practice to do that ?
Thank you.