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.

B473bd9d058c922e08d57f39a4a861db?s=128

Grégoire Hébert

March 29, 2019
Tweet

Transcript

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

    POC ! Faux !
  2. @gheb_dev Grégoire Hébert ‣Senior Developper & Trainer @ Les-Tilleuls.coop ‣CEO

    of MasterClass by Les-Tilleuls.coop @gheb_dev @gregoirehebert
  3. @gheb_dev Self Managed since 2011 36 people, + 1000% in

    6 years jobs@les-tilleuls.coop 13 full courses & custom courses masterclass@les-tilleuls.coop
  4. @gheb_dev R.A.D.

  5. @gheb_dev

  6. @gheb_dev

  7. @gheb_dev

  8. @gheb_dev

  9. @gheb_dev

  10. @gheb_dev R.A.D.

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

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

  13. @gheb_dev LvL 0: Always the same URL in POST with

    a 200 return status code. POST: /api
  14. @gheb_dev Richardson’s Maturity Chart

  15. @gheb_dev LvL 1: Each type of data has its own

    URL. POST: /books POST: /customers
  16. @gheb_dev Richardson’s Maturity Chart

  17. @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
  18. @gheb_dev Richardson’s Maturity Chart

  19. @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" } }
  20. @gheb_dev

  21. @gheb_dev V1 - DunglasJsonLdApiBundle Mars 2015

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    January 2017 V2 Stable Available February 2018 V2.2 March 2019 V2.4
  39. @gheb_dev First ApiResource

  40. @gheb_dev You’ve got the power

  41. @gheb_dev Some attributes

  42. @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… ?
  43. @gheb_dev Restricting the operations

  44. @gheb_dev Restricting the operations

  45. @gheb_dev Restricting the operations

  46. @gheb_dev Differentiating the expected income and the outcome

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

  48. @gheb_dev Differentiating the expected income and the outcome

  49. @gheb_dev ORM mapping

  50. @gheb_dev

  51. @gheb_dev R.A.D.

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

    !
  53. @gheb_dev

  54. @gheb_dev Activate Mercure for a resource

  55. @gheb_dev

  56. @gheb_dev

  57. @gheb_dev source: Samuel Roze « Symfony Messenger: Messages, Queues, Workers

    and More »
  58. @gheb_dev Activate messenger for a resource

  59. @gheb_dev Defining a Handler

  60. @gheb_dev

  61. @gheb_dev Configuring Messenger component

  62. @gheb_dev

  63. @gheb_dev

  64. @gheb_dev

  65. @gheb_dev

  66. @gheb_dev

  67. @gheb_dev Going Further

  68. @gheb_dev

  69. @gheb_dev Configure the workflow

  70. @gheb_dev Defining states constants

  71. @gheb_dev Configuration using constants

  72. @gheb_dev Configuration using constants

  73. @gheb_dev How to get for any resource a way to

    update the state ?
  74. @gheb_dev Leverage the extensibility of 
 API Platform

  75. @gheb_dev Decorating the ResourceMetadataFactory

  76. @gheb_dev Decorating the ResourceMetadataFactory

  77. @gheb_dev Inject the supported resources

  78. @gheb_dev Decorating the OperationPathResolver

  79. @gheb_dev Change the state on post deserialize

  80. @gheb_dev Change the state on post deserialize

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

  82. @gheb_dev Going Further (again)

  83. @gheb_dev Let’s consider we have Holder resource
 which is used

    as a User, with a JWT Authentication
  84. @gheb_dev Reverse side Linking a WaitingLine to a Holder

  85. @gheb_dev What if I want to set/remove ownership right after

    the change of state 
 with API Platform?
  86. @gheb_dev YOU DON’T.

  87. @gheb_dev Setting the ownership

  88. @gheb_dev Setting the ownership

  89. @gheb_dev Don’t forget to leverage the power of the Symfony

    components you use. They are highly tested !
  90. @gheb_dev A little demo maybe?

  91. @gheb_dev Going Further (again, again)
 promised, this is the last

    time
  92. @gheb_dev Sometimes, you need to split up your code. Let’s

    imagine a password request scenario
  93. @gheb_dev

  94. @gheb_dev

  95. @gheb_dev Get a resource

  96. @gheb_dev Get a resource

  97. @gheb_dev Create a resource

  98. @gheb_dev Create a resource

  99. @gheb_dev

  100. @gheb_dev Setting RabbitMQ as transporter

  101. @gheb_dev Creating the resource

  102. @gheb_dev Set messenger

  103. @gheb_dev Define the operations

  104. @gheb_dev Define the input and output

  105. @gheb_dev Define the input class

  106. @gheb_dev I can now reset my password

  107. @gheb_dev Create a resource Output is set to false, nothing

    to do We need to create the transformers
  108. @gheb_dev Transform the post input into a ressource

  109. @gheb_dev Maybe using the incoming AutoMapper there?

  110. @gheb_dev Encode the new password on PUT request

  111. @gheb_dev

  112. @gheb_dev Create the Handler

  113. @gheb_dev Handle the token creation

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

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

  116. @gheb_dev

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

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

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

  120. @gheb_dev And you’ve got a robust API For middle to

    large applications
  121. @gheb_dev THANK YOU ! I am so thirsty right now…

    But if you’ve got any question :)