Created
June 24, 2022 11:51
-
-
Save Nemo64/e135bb965470ee54f891d8f317122977 to your computer and use it in GitHub Desktop.
Good configuration examples for ApiPlatform Symfony project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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