User Tools

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

  • normalisation = from server to client
  • denormalisation = from client to server

/**
 * @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;

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also, you acknowledge that you have read and understand our Privacy Policy. If you do not agree, please leave the website.

More information