Table of Contents

Symfony Api Platform

quick

composer create-project symfony/skeleton bookshop-api
cd bookshop-api
composer require symfony/orm-pack
composer require --dev symfony/maker-bundle
composer req api

fill .env with database infos and then
bin/console doctrine:database:create
bin/console make:entity
#bin/console doctrine:schema:create
bin/console make:migration
bin/console doctrine:migrations:migrate
run test server
php -S 192.168.0.124:8000 -t public
frontend available at http://192.168.0.124:8000/api

notes

adding some infos

add title, description and version. in config/packages/api_platform.yaml

api_platform:
    title: "toto"
    description: "ben voila"
    version: "1.0"
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']
    patch_formats:
        json: ['application/merge-patch+json']
    swagger:
        versions: [3]

disable operations

in entity class header

 * @ApiResource(
 *     collectionOperations={"get"},
 *     itemOperations={"get", "put", "delete"}
 * )

read/write properties

in entity class header

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"article:read"}},
 *     denormalizationContext={"groups"={"article:write"}}
 * )
 * @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
 */
class Article
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     *
     * @Groups("article:read")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Groups({"article:read", "article:write"})
     */
    private $title;