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

API Platform version 2

API Platform version 2

Discover API Platform v2, a framework to create easily API-first applications.

E66449b8260b07a1cf51c5ab5eaa8180?s=128

Kévin Dunglas

May 17, 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 IS API PLATFORM? ROADMAP ▸ API Platform v1.1 ▸

    Released yesterday (2016-05-16) ▸ Bug fix and security only (no more new features) ▸ Shipped with Symfony 2.8, compatible with SF 3.0 ▸ End Of Life: Nov 2019 (= EOL of SF 2.8) ▸ API Platform v2.0 alpha 1 ▸ Released yesterday ▸ stable release: June (SF 3.1 release: May)
  10. WHAT IS API PLATFORM? FACT CHECKING ▸ First stable release:

    September 18, 2015 ▸ 660+ stars on GitHub ▸ 900+ commits ▸ 5 Core Team Members: Samuel Roze (Inviqa), Teoh Han Hui (Tripviss), Théo Fidry (Haircvt), Vincent Chalamon (Les-Tilleuls.coop) and me ▸ 60+ awesome contributors including Antoine Bluchet, Maxime Steinhausser, Hamza Amrouche, Jérémy Derussé and Charles Sarazin… ▸ Adopted by: Sensiolabs, Smile, The Fork/TripAdvisor, BeIN Sports, Tripviss, Eaux du Nord, Le Parti de Gauche, ExaqtWorld, Ville de Roubaix… And recently by some new major companies (announcement soon).
  11. WHAT’S NEW IN API PLATFORM 2?

  12. 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
  13. INSTALL WITH COMPOSER composer create-project -s alpha api-platform/api-platform app

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

    WORK IN PROGRESS
  15. CORE: NEW CONFIGURATION SYSTEM

  16. CORE: MINIMAL CONFIGURATION

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

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

    PROPERTY MAPPING
  19. AND YOU GET…

  20. CORE: NEW CONFIGURATION SYSTEM CREATE POST /tasks

  21. CORE: NEW CONFIGURATION SYSTEM VALIDATE POST /tasks

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

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

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

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

  26. CORE: NEW CONFIGURATION SYSTEM DOC (NELMIOAPIDOC)

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

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

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

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

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

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

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

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

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

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

  37. 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
  38. CORE: A NEW EVENT SYSTEM

  39. 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) ▸ Use kernel events to hook your custom logic: security (isGranted), cache headers, validation, persistence, call to external APIs, mail…
  40. CORE: A NEW EVENT SYSTEM THE REQUEST FLOW ▸ kernel.request:

    guess the best response format (content negotiation) ▸ controller: retrieve the data from the persistence system (GET, PUT, DELETE) ▸ controller: deserialize the request (POST, PUT) ▸ controller: return an entity (GET, POST, PUT, DELETE), an array of entities or a pagination (cGET) ▸ kernel.view (priority 20): validate data (POST, PUT) ▸ kernel.view (priority 10): persist data (POST, PUT, DELETE) ▸ kernel.view (priority 0): serialize data and transform them in a Response
  41. CORE: A NEW EVENT SYSTEM CREATING AN EVENT LISTENER

  42. CORE: A NEW EVENT SYSTEM REGISTERING THE EVENT LISTENER

  43. CORE: CUSTOM OPERATIONS AND CONTROLLERS

  44. 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 ▸ Always prefer kernel events when possible!
  45. CORE: CUSTOM OPERATIONS AND CONTROLLERS CUSTOMIZING OPERATIONS

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

  47. CORE: CUSTOM OPERATIONS AND CONTROLLERS REGISTER THE CONTROLLER IN THE

    ROUTING SYSTEM
  48. 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
  49. CORE: CONTENT NEGOTIATION

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

    format to use depending of the HTTP Accept header ▸ 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
  51. CORE: CONTENT NEGOTIATION WRITE A RESPONDER FOR THE FORMAT TO

    SUPPORT (HERE XML)
  52. CORE: CONTENT NEGOTIATION UPDATE THE CONFIGURATION AND REGISTER THE LISTENER

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

  54. SCHEMA GENERATOR: NEW FEATURES

  55. SCHEMA GENERATOR: NEW FEATURES ALSO IN SCHEMA GENERATOR 1.1 ▸

    The v1.1 of the Schema Generator is included in API Platform v1.1 and v2 ▸ Available as PHAR ▸ MongoDB support ▸ Symfony v3 compatibility ▸ Doctrine Embeddable support ▸ Support custom properties and entities ▸ Can leverage any RDF(a) vocabulary (lov.okfn.org) ▸ Serialization groups support ▸ Doctrine: unique, nullable and @Column can be customized
  56. SCHEMA GENERATOR: NEW FEATURES CUSTOM TYPES AND PROPERTIES

  57. SCHEMA GENERATOR: NEW FEATURES RESULT (EXTRACT)

  58. SCHEMA GENERATOR: NEW FEATURES RESULT (EXTRACT)

  59. SOME MORE THINGS

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

    MongoDB support (WIP) ▸ Pagination (configurable - new Hydra format) ▸ Relation handling ▸ New filters ▸ Complex type serialization (ex: nested objects) ▸ PSR-6 support (performance)
  61. WHAT’S NEW WITH API PLATFORM 2? CORE NEW IN THE

    SYMFONY SERIALIZER COMPONENT 3.1 ▸ data: URI to SplFileInfo normalizer for file upload ▸ \DateTime normalizer ▸ Nested object serialization ▸ Tree serialization with the @MaxDepth annotation ▸ Performance improvements ▸ PSR-6 support (performance)
  62. 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!
  63. 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/Gregcop1/hydra-admin ▸ [todo] Guzzle integration ▸ [todo] React / Angular 2 / Vue.js / … CRUD generators using the Hydra doc ▸ [todo] HAL and JSONAPI support (easy pick, thanks to the new architecture) ▸ [todo] Swagger 2.0 support (another easy pick)
  64. QUESTIONS?