Skip to content

Instantly share code, notes, and snippets.

@BillClinton
Last active April 20, 2020 19:36
Show Gist options
  • Save BillClinton/a2021683565c1047bcecb5b098816ac6 to your computer and use it in GitHub Desktop.
Save BillClinton/a2021683565c1047bcecb5b098816ac6 to your computer and use it in GitHub Desktop.
API Platform Notes

Setup

Create Project w/ version

composer create-project symfony/skeleton:^4.4 rest_api_project

Install MakerBundle (to use make command)

composer require symfony/maker-bundle --dev

Install API Platform

composer require api

Now when you create an entity with bin/console make:entity you will be asked if you would like make the class as an API resource

Configure DB connection

Create database and update connection string in .env file

DATABASE_URL=mysql://test-api-user:[email protected]:3306/test-api?serverVersion=5.7

  • Your API is almost ready:

    1. Create your first API resource in src/Entity;
    2. Go to /api to browse your API
  • To enable the GraphQL support, run composer require webonyx/graphql-php, then browse /api/graphql.

User/Auth configuration

Create user entity

bin/console make:user

Add user fields

bin/console make:entity

class name: User

Manually mark fields as unique=true, as necessary

@ORM\Column(type="string", length=255, unique=true)

Make and run migration

  • bin/console make:migration
  • bin/console doctrine:migrations:migrate

Dev

Start server

symfony serve

View current config

bin/console debug:config api_platform

See all config values

bin/console config:dump api_platform

Clear cache

bin/console cache:clear

Migrate

bin/console doctrine:migrations:migrate

or if that doesn't work because of a "table already exists" error:

bin/console doctrine:schema:update --force

Drop DB

doctrine:schema:drop --full-database --force

Testing

Install test suite (symfony/test-pack)

composer require test --dev

Update phpunit version in /phpunit.xml.dist

<server name="SYMFONY_PHPUNIT_VERSION" value="8.2" />

Require http-client

composer require symfony/http-client

Add a test DB

copy the .env DB config to .env.test and update DB name

DATABASE_URL=mysql://symfony-demo-user:[email protected]:3306/symfony-demo-test

create the test db

bin/console doctrine:database:create --env=test bin/console doctrine:schema:create --env=test

backport the API Platform 2.5 Test Tools

  • unnecessary if using API Platform >= v2.5

instructions: https://symfonycasts.com/screencast/api-platform-security/backport-api-tests

run tests

bin/phpunit

Install alice to create fixtures in YAML

composer require alice --dev

  • This installs handy ReloadDatabaseTrait

Install a logger

composer require logger

  • log will be available in var/log/test.log

Misc

encode a password

./bin/console security:encode-password

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment