Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Discovering API Platform v2 (updated version)

Discovering API Platform v2 (updated version)

Slides about the API Platform framework presented at the first "Apero PHP" in Amiens, France.

https://les-tilleuls.coop/fr/blog/article/save-the-date-1er-apero-php-a-amiens

E66449b8260b07a1cf51c5ab5eaa8180?s=128

Kévin Dunglas

July 07, 2016
Tweet

More Decks by Kévin Dunglas

Other Decks in Programming

Transcript

  1. API PLATFORM 2.0 MODERN AND POWERFUL WEB APIS

  2. ABOUT ME KÉVIN DUNGLAS ▸ Founder of Les-Tilleuls.coop ▸ Symfony

    Core Team Member ▸ API Platform Creator ▸ Teacher at Lille 1 University Twitter / GitHub : @dunglas dunglas.fr
  3. ABOUT US LES-TILLEULS.COOP ▸ 15+ consultants and developers ▸ Symfony

    / Angular 1 & 2 / React / Amazon AWS / Docker / SCRUM / … ▸ 100% owned by his employees ▸ 1 person = 1 vote, all benefits are equally distributed ▸ We are hiring! Twitter / GitHub : @coopTilleuls les-tilleuls.coop
  4. WHAT IS API PLATFORM?

  5. A READY-TO-USE SYMFONY EDITION TO CREATE MODERN WEB APIS. WHAT

    IS API PLATFORM?
  6. A SET OF STANDALONE COMPONENTS DEDICATED TO API-FIRST PROJECTS. WHAT

    IS API PLATFORM?
  7. WHAT IS API PLATFORM? THE GOALS OF THE FRAMEWORK ▸

    Create web APIs easily (focused on Developer eXperience) ▸ Develop a fully-featured API in minutes ▸ Override and customize everything you need ▸ Be compliant out of the box with the last generation of standards and usages ▸ Linked Data, HATEOAS and semantic web support : JSON-LD, Hydra, schema.org, JWT…
  8. ARCHITECTURE, PATTERNS, PHILOSOPHY, BENEFITS AND (THE BUNCH OF) EXISTING FEATURES:

    DUNGLAS.FR/SLIDES/FORUM-PHP-2015/ ACCURATE FOR V1 AND 2!
  9. WHAT’S NEW IN API PLATFORM 2?

  10. DISCOVERING API PLATFORM 2.0 ARCHITECTURAL CHANGES ▸ Not a full

    rewrite: fix design limitations from v1 - but contains BC breaks! ▸ « API Platform Core » (formerly DunglasApiBundle) is not a Symfony bundle anymore but a standalone PHP library built with SF components ▸ Usable in raw PHP (Laravel support is coming) ▸ Symfony still the first-class citizen (bundle included) ▸ Implements the Action-Domain-Responder pattern using events of the Symfony kernel (more flexible, easier to customize) ▸ PHP 7+: use new features such as the ?? operator, scalar type hints and return types ▸ Symfony 3.1+: a lof of code from v1 have been moved to SF components: Serializer, PropertyInfo, PropertyAccess
  11. INSTALL WITH COMPOSER composer create-project -s beta api-platform/api-platform app

  12. INSTALL WITH DOCKER git clone git@github.com:api-platform/core.git docker-compose up

  13. CORE: NEW CONFIGURATION SYSTEM

  14. CORE: MINIMAL CONFIGURATION

  15. CORE: NEW CONFIGURATION SYSTEM XML SUPPORT TODO: ADD SUPPORT FOR

    PROPERTY MAPPING
  16. CORE: NEW CONFIGURATION SYSTEM YAML SUPPORT TODO: ADD SUPPORT FOR

    PROPERTY MAPPING
  17. AND YOU GET…

  18. CORE: NEW CONFIGURATION SYSTEM CREATE POST /tasks

  19. CORE: NEW CONFIGURATION SYSTEM VALIDATE POST /tasks

  20. CORE: NEW CONFIGURATION SYSTEM RETRIEVE (COLLECTION) GET /tasks

  21. CORE: NEW CONFIGURATION SYSTEM RETRIEVE (ITEM) GET /tasks/1

  22. CORE: NEW CONFIGURATION SYSTEM UPDATE PUT /tasks/1

  23. CORE: NEW CONFIGURATION SYSTEM DELETE DELETE /tasks/1

  24. CORE: NEW CONFIGURATION SYSTEM DOC (NELMIOAPIDOC)

  25. CORE: NEW CONFIGURATION SYSTEM HYPERMEDIA (HYDRA) API ENTRYPOINT GET /

  26. CORE: NEW CONFIGURATION SYSTEM AUTODISCOVERABILITY (HYDRA) GET /apidoc (extract)

  27. CORE: NEW CONFIGURATION SYSTEM AND MORE ▸ Pagination ▸ Relations

    ▸ Sorting ▸ Filtering ▸ …
  28. CORE: NEW FILTERS CONFIGURATION

  29. CORE: NEW FILTERS CONFIGURATION REGISTER A BUILT-IN FILTER AS A

    SERVICE
  30. CORE: NEW FILTERS CONFIGURATION UPDATE THE RESOURCE CONFIGURATION

  31. CORE: NEW FILTERS CONFIGURATION QUERY GET /tasks?text=somet

  32. CORE: NEW FILTERS CONFIGURATION AVAILABLE FILTERS (DOCTRINE) ▸ Search (LIKE

    %%) ▸ Order (sorting) ▸ Date ▸ Range ▸ Boolean ▸ Numeric
  33. CORE: OPERATIONS AND SERIALIZATION GROUPS

  34. CORE: OPERATIONS AND SERIALIZATION GROUPS CUSTOMIZING OPERATIONS AND SERIALIZATION GROUPS

  35. CORE: OPERATIONS AND SERIALIZATION GROUPS WHAT HAPPENS? ▸ Only GET

    /tasks and GET /tasks/{id} are available ▸ All other operations (POST, PUT, DELETE) are not registered ▸ When using the configuration, declaration must be explicits ▸ Different serialization groups are applied per operation ▸ Only « text » is displayed in the list ▸ « text » and « status » are displayed for the item IRI ▸ Deserialization isn’t impacted (use the denormalization_context key) ▸ Works with XML and YAML mappings too
  36. CORE: A NEW EVENT SYSTEM

  37. CORE: A NEW EVENT SYSTEM A NEW EVENT SYSTEM ▸

    The event system of v1 has been removed (PRE_CREATE, POST_CREATE, PRE_VALIDATE…) ▸ V2 now relies on Symfony HttpKernel events ▸ V2 implements the Action-Domain-Responder pattern ▸ Events listeners (see next slide) are only applied on request handled by API Platform (not on standard Symfony requests) ▸ Event listeners are autoregistered and autowired (using DunglasActionBundle) ▸ Use kernel events to hook your custom logic: security (isGranted), cache headers, validation, persistence, call to external APIs, mail…
  38. CORE: A NEW EVENT SYSTEM THE REQUEST FLOW

  39. CORE: A NEW EVENT SYSTEM CREATING AN EVENT SUBSCRIBER

  40. CORE: CUSTOM OPERATIONS AND CONTROLLERS

  41. CORE: CUSTOM OPERATIONS AND CONTROLLERS WHY USING CUSTOM OPERATIONS AND

    CONTROLLERS? ▸ Create service-oriented (or non-REST) endpoints ▸ Customize URLs ▸ Manage sub-resources (ex: /tasks/{id}/comments/{c_id}) ▸ Hook your own logic when kernel events are not enough ▸ Controllers and actions are autoregistered and autowired (DunglasActionBundle) ▸ Always prefer kernel events when possible!
  42. CORE: CUSTOM OPERATIONS AND CONTROLLERS CUSTOMIZING OPERATIONS

  43. CORE: CUSTOM OPERATIONS AND CONTROLLERS CREATING THE CUSTOM CONTROLLER

  44. CORE: CUSTOM OPERATIONS AND CONTROLLERS OTHER EXTENSION POINTS (EXIST IN

    V1) ▸ Filters: manipulate the HTTP request and Doctrine (if used) queries (filtering, sorting…) ▸ Data Providers: retrieve data from the persistence layer ▸ Normalizers (Symfony Serializer) ▸ Encoders (Symfony Serializer) ▸ Validators (Symfony Validation) ▸ All Symfony (services, config…) and Doctrine (if used) extension points
  45. CORE: CONTENT NEGOTIATION

  46. CORE: CONTENT NEGOTIATION CORE: CONTENT NEGOTIATION ▸ Select the best

    format to use depending of the HTTP Accept header ▸ Built-in support for JSON-LD, XML, JSON and CSV ▸ Built on top of William Durand’s Negotiation 2 library (same as FOSRest) ▸ Hooks into the Symfony Serializer component ▸ Allows to return hypermedia (HAL, JSON-API…) and non hypermedia (raw JSON, raw XML…) formats
  47. CORE: CONTENT NEGOTIATION ENABLING XML (FOR INSTANCE)

  48. CORE: CONTENT NEGOTIATION QUERY THE API GET /tasks/1
 Accept: text/xml

  49. SOME MORE THINGS

  50. CORE: NEW CONFIGURATION SYSTEM MORE NEW API PLATFORM FEATURES ▸

    Swagger 2.0 support ▸ Configurable Pagination ▸ Relation handling ▸ Some new filters ▸ Complex type serialization (ex: nested objects) ▸ PSR-6 cache support (performance)
  51. WHAT’S NEW WITH API PLATFORM 2? CORE NEW IN THE

    SYMFONY SERIALIZER COMPONENT 3.1/3.2 ▸ data: URI to SplFileInfo normalizer for file upload ▸ \DateTime normalizer ▸ Nested object serialization ▸ Tree serialization with the @MaxDepth annotation ▸ CSV support ▸ Performance improvements ▸ PSR-6 support (performance)
  52. DISCOVERING API PLATFORM 2.0 YOU CAN HELP US! ▸ Finish

    and improve the doc: api-platform/doc#37 ▸ Finish MongoDB support: api-platform/core#461 ▸ Test, test and test again ▸ Improve the website: github.com/api-platform/website ▸ Spread the word!
  53. DISCOVERING API PLATFORM 2.0 WHAT’S NEXT (2.1, 2.2…) ▸ Client-side

    tools ▸ [alpha] ng-admin / Hydra bridge: github.com/api-platform/admin ▸ [WIP] Angular 2 Hydra admin: github.com/coopTilleuls/hm-admin ▸ [todo] Guzzle integration trough Jane (JoliCode) ▸ [todo] React / Angular 2 / Vue.js / … CRUD generators using the Hydra doc ▸ [todo] HAL and JSONAPI support (easy pick, thanks to the new architecture)
  54. QUESTIONS?