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

leçon n°139, API Platform ce n'est que pour du POC, FAUX !

leçon n°139, API Platform ce n'est que pour du POC, FAUX !

La première chose qui est mise en avant avec API Platform c'est la capacité de faire du développement rapide d'application. C'est vrai. Ça ne signifie pas pour autant qu'il est impossible de réaliser une application complexe avec une séparation de la couche métier, de l'api publique. Voyons ensemble les mécanismes avancés d'API Platform et les outils qui en feront votre framework API en PHP.

Grégoire Hébert

March 29, 2019
Tweet

More Decks by Grégoire Hébert

Other Decks in Programming

Transcript

  1. @gheb_dev
    Leçon n°139, API-Platform
    Ce n’est bon qu’à faire un POC !
    Faux !

    View full-size slide

  2. @gheb_dev
    Grégoire Hébert
    ‣Senior Developper & Trainer @ Les-Tilleuls.coop
    ‣CEO of MasterClass by Les-Tilleuls.coop
    @gheb_dev
    @gregoirehebert

    View full-size slide

  3. @gheb_dev
    Self Managed since 2011
    36 people, + 1000% in 6 years
    [email protected]
    13 full courses & custom courses
    [email protected]

    View full-size slide

  4. @gheb_dev
    R.A.D.

    View full-size slide

  5. @gheb_dev
    R.A.D.

    View full-size slide

  6. @gheb_dev
    Let’s see why it’s worth it!

    View full-size slide

  7. @gheb_dev
    Richardson’s Maturity Chart
    Graphql I see you…

    View full-size slide

  8. @gheb_dev
    LvL 0: Always the same URL in POST with a
    200 return status code.
    POST: /api

    View full-size slide

  9. @gheb_dev
    Richardson’s Maturity Chart

    View full-size slide

  10. @gheb_dev
    LvL 1: Each type of data has its own URL.
    POST: /books
    POST: /customers

    View full-size slide

  11. @gheb_dev
    Richardson’s Maturity Chart

    View full-size slide

  12. @gheb_dev
    LvL 2: We use specific verbs for specific
    actions with status codes according to the
    result.
    GET: /books
    POST: /books
    PUT: /books/12

    View full-size slide

  13. @gheb_dev
    Richardson’s Maturity Chart

    View full-size slide

  14. @gheb_dev
    LvL 3: Hateoas - Hypermedia As The Engine
    Of Application State. We add links to describe
    how to get to the information in the answer.
    {
    "@context": {
    "comments": "http://api.example.com/vocab#comments"
    },
    "@id": "http://api.example.com/an-issue",
    "title": "An exemplary issue linking to its comments",
    "comments": { "@id": "http://api.example.com/an-issue/comments" }
    }

    View full-size slide

  15. @gheb_dev
    V1 - DunglasJsonLdApiBundle
    Mars 2015

    View full-size slide

  16. @gheb_dev
    V1 - DunglasJsonLdApiBundle
    Mars 2015
    Juin 2015 Dedicated Website

    View full-size slide

  17. @gheb_dev
    V1 - DunglasJsonLdApiBundle
    Mars 2015
    Juin 2015 Dedicated Website

    View full-size slide

  18. @gheb_dev
    V1 - DunglasJsonLdApiBundle
    Mars 2015
    Juin 2015 Dedicated Website

    View full-size slide

  19. @gheb_dev
    V1 - DunglasJsonLdApiBundle
    Mars 2015
    Juin 2015 Dedicated Website
    Septembre 2015 V2 Annonced

    View full-size slide

  20. @gheb_dev
    V1 - DunglasJsonLdApiBundle
    Mars 2015
    Juin 2015 Dedicated Website
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available

    View full-size slide

  21. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available

    View full-size slide

  22. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available
    January 2017 V2 Stable Available

    View full-size slide

  23. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available
    January 2017 V2 Stable Available

    View full-size slide

  24. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available
    January 2017 V2 Stable Available

    View full-size slide

  25. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available
    January 2017 V2 Stable Available
    V2.2
    February 2018

    View full-size slide

  26. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available
    January 2017 V2 Stable Available
    V2.2
    February 2018

    View full-size slide

  27. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available
    January 2017 V2 Stable Available
    V2.2
    February 2018

    View full-size slide

  28. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available
    January 2017 V2 Stable Available
    V2.2
    February 2018

    View full-size slide

  29. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available
    January 2017 V2 Stable Available
    February 2018 V2.2
    March 2019 V2.4

    View full-size slide

  30. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available
    January 2017 V2 Stable Available
    February 2018 V2.2
    February 2019 V2.4

    View full-size slide

  31. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available
    January 2017 V2 Stable Available
    February 2018 V2.2
    February 2019 V2.4

    View full-size slide

  32. @gheb_dev
    Septembre 2015 V2 Announced
    Mai 2016 V2 Alpha Available
    January 2017 V2 Stable Available
    February 2018 V2.2
    March 2019 V2.4

    View full-size slide

  33. @gheb_dev
    First ApiResource

    View full-size slide

  34. @gheb_dev
    You’ve got the power

    View full-size slide

  35. @gheb_dev
    Some attributes

    View full-size slide

  36. @gheb_dev
    Any Public, Protected, Private property will be accessible through your API 

    As long as you define the means to access it !
    THANKS TO… ?

    View full-size slide

  37. @gheb_dev
    Restricting the operations

    View full-size slide

  38. @gheb_dev
    Restricting the operations

    View full-size slide

  39. @gheb_dev
    Restricting the operations

    View full-size slide

  40. @gheb_dev
    Differentiating the expected income and the outcome

    View full-size slide

  41. @gheb_dev
    Source: https://symfony.com/doc/current/components/serializer.html

    View full-size slide

  42. @gheb_dev
    Differentiating the expected income and the outcome

    View full-size slide

  43. @gheb_dev
    ORM mapping

    View full-size slide

  44. @gheb_dev
    R.A.D.

    View full-size slide

  45. @gheb_dev
    Ok, but not everything is about
    CRUD or REST !

    View full-size slide

  46. @gheb_dev
    Activate Mercure for a resource

    View full-size slide

  47. @gheb_dev
    source: Samuel Roze « Symfony Messenger: Messages, Queues, Workers and More »

    View full-size slide

  48. @gheb_dev
    Activate messenger for a resource

    View full-size slide

  49. @gheb_dev
    Defining a Handler

    View full-size slide

  50. @gheb_dev
    Configuring Messenger component

    View full-size slide

  51. @gheb_dev
    Going Further

    View full-size slide

  52. @gheb_dev
    Configure the workflow

    View full-size slide

  53. @gheb_dev
    Defining states constants

    View full-size slide

  54. @gheb_dev
    Configuration using constants

    View full-size slide

  55. @gheb_dev
    Configuration using constants

    View full-size slide

  56. @gheb_dev
    How to get for any resource a
    way to update the state ?

    View full-size slide

  57. @gheb_dev
    Leverage the extensibility of 

    API Platform

    View full-size slide

  58. @gheb_dev
    Decorating the ResourceMetadataFactory

    View full-size slide

  59. @gheb_dev
    Decorating the ResourceMetadataFactory

    View full-size slide

  60. @gheb_dev
    Inject the supported resources

    View full-size slide

  61. @gheb_dev
    Decorating the OperationPathResolver

    View full-size slide

  62. @gheb_dev
    Change the state on post deserialize

    View full-size slide

  63. @gheb_dev
    Change the state on post deserialize

    View full-size slide

  64. @gheb_dev
    I get a new operation to update the state

    View full-size slide

  65. @gheb_dev
    Going Further (again)

    View full-size slide

  66. @gheb_dev
    Let’s consider we have Holder resource

    which is used as a User, with a JWT
    Authentication

    View full-size slide

  67. @gheb_dev
    Reverse side
    Linking a WaitingLine to a Holder

    View full-size slide

  68. @gheb_dev
    What if I want to set/remove ownership
    right after the change of state 

    with API Platform?

    View full-size slide

  69. @gheb_dev
    YOU DON’T.

    View full-size slide

  70. @gheb_dev
    Setting the ownership

    View full-size slide

  71. @gheb_dev
    Setting the ownership

    View full-size slide

  72. @gheb_dev
    Don’t forget to leverage the power of
    the Symfony components you use.
    They are highly tested !

    View full-size slide

  73. @gheb_dev
    A little demo maybe?

    View full-size slide

  74. @gheb_dev
    Going Further (again, again)

    promised, this is the last time

    View full-size slide

  75. @gheb_dev
    Sometimes, you need to split up your code.
    Let’s imagine a password request scenario

    View full-size slide

  76. @gheb_dev
    Get a resource

    View full-size slide

  77. @gheb_dev
    Get a resource

    View full-size slide

  78. @gheb_dev
    Create a resource

    View full-size slide

  79. @gheb_dev
    Create a resource

    View full-size slide

  80. @gheb_dev
    Setting RabbitMQ as transporter

    View full-size slide

  81. @gheb_dev
    Creating the resource

    View full-size slide

  82. @gheb_dev
    Set messenger

    View full-size slide

  83. @gheb_dev
    Define the operations

    View full-size slide

  84. @gheb_dev
    Define the input and output

    View full-size slide

  85. @gheb_dev
    Define the input class

    View full-size slide

  86. @gheb_dev
    I can now reset my password

    View full-size slide

  87. @gheb_dev
    Create a resource
    Output is set to false, nothing to do
    We need to create the transformers

    View full-size slide

  88. @gheb_dev
    Transform the post input into a ressource

    View full-size slide

  89. @gheb_dev
    Maybe using the incoming AutoMapper there?

    View full-size slide

  90. @gheb_dev
    Encode the new password on PUT request

    View full-size slide

  91. @gheb_dev
    Create the Handler

    View full-size slide

  92. @gheb_dev
    Handle the token creation

    View full-size slide

  93. @gheb_dev
    Handle the password update and remove the token

    View full-size slide

  94. @gheb_dev
    Add a custom constraint to check the expiration

    View full-size slide

  95. @gheb_dev
    You’ve got a fully decoupled
    reset password procedure !

    View full-size slide

  96. @gheb_dev
    Spice all this with your favorites
    Testing frameworks

    View full-size slide

  97. @gheb_dev
    Plug-in your favorite CI / CD
    system

    View full-size slide

  98. @gheb_dev
    And you’ve got a robust API
    For middle to large applications

    View full-size slide

  99. @gheb_dev
    THANK YOU !
    I am so thirsty right now…
    But if you’ve got any question :)

    View full-size slide