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

Create Symfony apps as quickly as with Laravel, keep your code framework-agnostic

Create Symfony apps as quickly as with Laravel, keep your code framework-agnostic

Slides of my talk at AFUP's Forum PHP 2016.

You’ll learn about RAD and autowiring, the ADR pattern, framework agnostic controllers, PSR-7, PSR-15 and PSR-17.

Kévin Dunglas

October 28, 2016
Tweet

More Decks by Kévin Dunglas

Other Decks in Programming

Transcript

  1. Kévin Dunglas •Founder of Les-Tilleuls.coop •Symfony Core Team member •API

    Platform creator •Teacher at the University of Lille 1 @dunglas
  2. Les-Tilleuls.coop •Self-managed company since 2011 •100% owned by employees •All

    benefits are equitably shared between employees •18 people, 137% growth in 2015 •We are hiring! => [email protected]
  3. The Symfony Book « A controller is a PHP function

    […] that reads information from the Symfony's Request object and creates and returns a Response object. »
  4. Controllers are Too Fat •Tend to put Domain / business

    logic in the controller (bad practice) Creating services requires some Symfony skills •Methods with too much lines of code (#previousLine) •Bloated controller classes with a lot of methods •Duplicated code (e.g. to generate a Response with HTTP headers, to call the view…)
  5. Controllers break OOP Best Practices •Magic and implicit •Container aware:

    no explicit dependencies •Hard to write SOLID controller classes •Hard to unit test controller classes •God objects (too much dependencies) Some people say « it’s OK for controllers »
  6. Controllers are Coupled to SF It matters only for library

    developers: •Hard to reuse controller classes across projects •Cannot work with several frameworks •Cannot be distributed as standalone libs •Don’t support « standards » (PSR-7, PSR-15, PSR-17)
  7. The ADR pattern •Stand for Action-Domain-Responder •Web-specific refinement of MVC

    •A partial solution to previous problems https://github.com/pmjones/adr
  8. The ADR pattern •Action: Connects Domain and Responder. Uses the

    request input to interact with the Domain, passes the Domain output to the responder. •Domain: The app’s business logic. Modifies states, persistence. Manipulates session and env data. •Responder: Build an HTTP response. Body content, templates, views, cookies, status code and so on.
  9. Pros •Better design and structure (= easier maintenance and better

    adaptability) •Easier to unit test (including actions and responders, thanks to explicit dependencies) •All chunks of code are independently reusables •No more duplicated code (e.g. headers) •Agnostic of the (full stack) framework: classes depend only of components (standalone libs)
  10. Cons •Worst DX: lot of configuration files (YAML or XML)

    •Harder to refactor (RAD, prototyping…): require to update the config all the time •Mandatory Symfony skills: DIC configuration, controllers as a services, YAML/XML routing…
  11. The DIC Autowiring Subsystem •Guesses, builds and injects automatically dependencies

    of a service using PHP’s reflection API •If a service of the given type exists: it is injected •If it doesn’t exist: it is created then injected •Allows to map an interface to a default implementation •SF 2.8+, will support method injection in 3.2 •No performance impact at runtime
  12. A Better Developper eXperience •As easy as the the full

    stack framework’s base controller •No need for magic proxy methods (getDoctrine(), json()…) •Easier refactoring: create new classes, add new dependencies, they are automatically built and injected with 0 config (Laravel-like)
  13. The Action Bundle •« Replacement » for the whole Symfony

    controller layer: actions, commands, event subscribers •Action classes, commands and subscribers are automatically registered as services •Autowiring is on for all those services •Configurable (scanned directories, interfaces, tags…) •250 lines of code for 180 GitHub stars (best ratio ever)
  14. The Action Bundle Included by default in API Platform 2

    And maybe in a future Symfony version?
  15. PSR-7 and PSR-17 • PSR-7: HTTP Message Interface • PSR-17:

    HTTP Factories composer require symfony/psr-http-message-bridge zendframework/zend-diactoros http-interop/http-factory:dev-master http-interop/http-factory-diactoros:dev-master
  16. Summary • Controller Autowiring and the Action Bundle improve the

    DX and ease the refactoring • ADR gives more structure to your app and is better from a design point of view • If you are a library developper, you can create standalone, framework agnostic actions working with Symfony and using PSR-7-15-17