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

ADR pattern, autowiring, DunglasActionBundle: Symfony controllers redesigned

ADR pattern, autowiring, DunglasActionBundle: Symfony controllers redesigned

Presentation of the controller system of Symfony and one of is refinement using the ADR pattern and services autowiring: DunglasActionBundle.

https://github.com/dunglas/DunglasActionBundle

E66449b8260b07a1cf51c5ab5eaa8180?s=128

Kévin Dunglas

June 16, 2016
Tweet

More Decks by Kévin Dunglas

Other Decks in Programming

Transcript

  1. ACTIONBUNDLE SYMFONY CONTROLLERS, REDESIGNED

  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. A CONTROLLER IS A PHP CALLABLE YOU CREATE THAT TAKES

    INFORMATION FROM THE HTTP REQUEST AND CREATES AND RETURNS AN HTTP RESPONSE (AS A SYMFONY RESPONSE OBJECT). The Symfony Book SYMFONY CONTROLLERS
  5. SYMFONY CONTROLLERS, NOWADAYS

  6. SYMFONY CONTROLLERS TYPICAL CONTROLLER

  7. SYMFONY CONTROLLERS FUNCTION AS CONTROLLER

  8. SYMFONY CONTROLLERS INVOKABLE CLASS AS CONTROLLER

  9. SYMFONY CONTROLLERS INVOKABLE CLASS: HOW DOES IT WORK?

  10. SYMFONY CONTROLLERS CONTAINER AWARE CONTROLLER (E.G. FRAMEWORKBUNDLE’S CONTROLLER)

  11. SYMFONY CONTROLLERS CONTROLLER AS A SERVICE

  12. SYMFONY CONTROLLERS CONTROLLER AS A SERVICE

  13. SYMFONY CONTROLLERS INVOKABLE CONTROLLER AS A SERVICE

  14. AUTOWIRED SERVICES

  15. AUTOWIRING ALLOWS TO REGISTER SERVICES IN THE CONTAINER WITH MINIMAL

    CONFIGURATION. IT AUTOMATICALLY RESOLVES THE SERVICE DEPENDENCIES BASED ON THE CONSTRUCTOR'S TYPEHINT WHICH IS USEFUL IN THE FIELD OF RAPID APPLICATION DEVELOPMENT, WHEN DESIGNING PROTOTYPES IN EARLY STAGES OF LARGE PROJECTS. IT MAKES IT EASY TO REGISTER A SERVICE GRAPH AND EASES REFACTORING. The Symfony Book AUTOWIRED SERVICES
  16. AUTOWIRED SERVICES EXAMPLE

  17. DUNGLAS ACTION BUNDLE

  18. DUNGLASACTIONBUNDLE ALLOWS TO CREATE REUSABLE, FRAMEWORK AGNOSTIC AND EASY TO

    UNIT TEST ACTION CLASSES. github.com/dunglas/DunglasActionBundle TEXTE
  19. DUNGLASACTIONBUNDLE INTRODUCTION ▸ Replacement for the controller layer (controllers, commands,

    event listeners…) of Symfony ▸ Controllers and commands automatically registered as services ▸ Autowiring enabled: their dependencies are explicitly injected - you don’t need to configure your services anymore!! ▸ Only 209 lines of code (+ some patches to Symfony) ▸ 150+ stars on GitHub
  20. DUNGLASACTIONBUNDLE INSTALL composer require dunglas/action-bundle

  21. DUNGLASACTIONBUNDLE USAGE (CONTROLLER)

  22. NO CONFIGURATION REQUIRED TO INJECT DEPENDENCIES! TEXTE

  23. AUTOWIRED SERVICES EXAMPLE

  24. DUNGLASACTIONBUNDLE USAGE (COMMAND)

  25. DUNGLASACTIONBUNDLE MORE? ▸ Also support Event Listeners ▸ Configuration to

    register your own types of classes ▸ Can automatically add tags to registered classes ▸ 100% compatible with existing libraries and bundles (including SensioFrameworkExtraBundle) ▸ Will be included in API Platform v2
  26. THE ADR PATTERN

  27. THE ADR PATTERN INTRODUCTION ▸ Stand for Action-Domain-Responder ▸ Web-specific

    refinement of Model-View-Controller https://github.com/pmjones/adr
  28. THE ADR PATTERN DETAILS ▸ Action: logic that connects the

    Domain and Responder. Uses the request input to interact with the Domain, and passes the Domain output to the Responder. ▸ Domain: logic to manipulate the domain, session, application, and environment data, modifying state and persistence as needed. ▸ Responder: logic to build an HTTP response or response description. It deals with body content, templates and views, headers and cookies, status codes, and so on. Source: https://github.com/pmjones/adr
  29. AN ADR IMPLEMENTATION USING DUNGLASACTIONBUNDLE

  30. AN ADR IMPLEMENTATION USING DUNGLASACTIONBUNDLE ACTION

  31. AN ADR IMPLEMENTATION USING DUNGLASACTIONBUNDLE RESPONDER

  32. DUNGLASACTIONBUNDLE THE ROAD TO REUSABLE AND CROSS FRAMEWORK ACTIONS ▸

    Don’t depend of FrameworkBundle features ▸ Use PSR-7 instead of HttpFoundation: https:// dunglas.fr/2015/07/slides-using-psr-7-with- symfony/ ▸ Don’t use annotations (prefer XML or YAML for routing)
  33. DUNGLASACTIONBUNDLE GIVE IT A STAR ON GITHUB https://github.com/dunglas/DunglasActionBundle

  34. QUESTIONS?