Skip to content

Instantly share code, notes, and snippets.

@Nemo64
Created June 24, 2022 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nemo64/e135bb965470ee54f891d8f317122977 to your computer and use it in GitHub Desktop.
Save Nemo64/e135bb965470ee54f891d8f317122977 to your computer and use it in GitHub Desktop.
Good configuration examples for ApiPlatform Symfony project
api_platform:
# [...] leave the default configuration
defaults:
# only use pagination if explicitly requested
# otherwise, you'll just ignore it and have a broken application once you have more than 10 items
pagination_enabled: true
# i assume all endpoints require _some_ authentication
# overwrite it per resource if necessary
security: 'is_granted("IS_AUTHENTICATED_FULLY")'
# I just define default serialization groups here
# feel free to overwrite them on an entity level if necessary
normalization_context: { groups: [ read ] }
denormalization_context: { groups: [ write ] }
# you usually expect the newest items to come first from an api response
# since sorting by id does not cost anything in mysql, I just sort by id descending
order: {"id": "desc"}
# add default security checks to all item operations
# (also drop the PATCH operation that I never use or test)
itemOperations:
get:
security: 'is_granted("read", object)'
put:
security: 'is_granted("read", object)'
security_post_denormalize: 'is_granted("write", object)'
delete:
security: 'is_granted("write", object)'
# Collection operations are a bit complicated when it comes to ApiPlatform security
# There can't be an "object" (since it would be too compute heavy) so it needs to be solved differently
# more here: https://medium.marco.zone/doctrine-symfony-centralized-access-control-d1f4717734e5
collectionOperations:
get:
post:
security_post_denormalize: 'is_granted("write", object)'
composer require \
doctrine/orm \
doctrine/doctrine-bundle \
doctrine/doctrine-migrations-bundle \
symfony/validator \
api-platform/core \
damienharper/auditor-bundle \
phpdocumentor/reflection-docblock \
symfony/twig-bundle
composer require --dev \
symfony/maker-bundle \
roave/security-advisories:dev-latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment