Symfony2 Best Practices From The Trenches

Symfony2 Best Practices From The Trenches

Symfony2 is a great framework and getting your first application up is easy. The documentation of Symfony2 is good, but there's only so much that documentation can teach you. Many details and best practices are best learned while you're working on your project. During this talk, you will be bombarded with those small pieces of knowledge and experience learned from the trenches of actual Symfony2 projects, where developers had to dodge those bullets Matrix-style. Whether it is about that little configuration detail you always forget or a good way of abstracting logic into the right pieces, we'll cover it all.

A8f72e32766355f12a56ede9aaa0ee78?s=128

Stefan Koopmanschap

October 01, 2014
Tweet

Transcript

  1. 1.

    F R O M T H E T R E

    N C H E S S Y M F O N Y 2 B E S T P R A C T I C E S
  2. 2.

    S T E FA N K O O P M

    A N S C H A P • PHPBenelux • PFZ • Ingewikkeld • phpBB, Zend Framework, Symfony2, joind.in, protalk, etc
  3. 4.

    O U R T O P I C S F

    O R T O D AY • Dependency Injection • Service Layer • The Symfony2 Documentation • Project Configuration • Service Configuration
  4. 5.

    O U R T O P I C S F

    O R T O D AY • Choose and standardise • Composer • What else?
  5. 6.
  6. 7.
  7. 8.

    D E P E N D E N C Y

    I N J E C T I O N
  8. 9.

    D E P E N D E N C Y

    I N J E C T I O N • No hardcoded dependencies • Easily manage and update specific classes • Minimize bootstrap code • More testable code
  9. 10.

    D E P E N D E N C Y

    I N J E C T I O N class Foo { public function bar() { $coffee = new Coffee(); $coffee->init(); return $coffee->drink(); } }
  10. 11.

    D E P E N D E N C Y

    I N J E C T I O N class Foo { public function bar() { $coffee = new Coffee(); $coffee->init(); return $coffee->drink(); } }
  11. 12.

    D E P E N D E N C Y

    I N J E C T I O N class Foo { public function bar() { $coffee = new Coffee(); $coffee->init(); return $coffee->drink(); } }
  12. 13.

    D E P E N D E N C Y

    I N J E C T I O N class Foo { public function bar() { $coffee = new Coffee(); $coffee->init(); return $coffee->drink(); } }
  13. 14.

    D E P E N D E N C Y

    I N J E C T I O N class Coffee implements Roastable {} class Foo { private $coffee; public function __construct(Roastable $coffee) { $this->coffee = $coffee; } }
  14. 15.

    D E P E N D E N C Y

    I N J E C T I O N class Foo { private $coffee; public function bar() { return $this->coffee->drink(); } }
  15. 16.

    D E P E N D E N C Y

    I N J E C T I O N <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/ services-1.0.xsd"> ! <parameters> <parameter key="coffee.class">Coffee</parameter> <parameter key="foo.class">Foo</parameter> </parameters> ! <services> <service id="coffee" class="%coffee.class%"> </service> ! <service id="foo" class="%foo.class%"> <argument type="service" id="coffee" /> </service> </services> ! </container>
  16. 17.

    D E P E N D E N C Y

    I N J E C T I O N class DefaultController extends ApiControllerAbstract { public function fooController() { $foo = $this->get('foo'); $foo->bar(); } }
  17. 19.

    S E R V I C E L AY E

    R • Abstract data store from business logic • Seperation of concerns • Easy access via service container
  18. 20.

    S E R V I C E L AY E

    R • Abstract data store from business logic • Seperation of concerns • Easy access via service container
  19. 21.

    S E R V I C E L AY E

    R C O N T R O L L E R S E R V I C E L AY E R P E R S I S T E N C Y L AY E R
  20. 22.

    S E R V I C E L AY E

    R Take it even further: ! http://protalk.me/the-framework-as-an- implementation-detail
  21. 23.

    T H E S Y M F O N Y

    2 D O C U M E N TAT I O N
  22. 24.

    T H E S Y M F O N Y

    2 D O C U M E N TAT I O N • The best starting point for your search • Not the ultimate source for information
  23. 25.

    T H E S Y M F O N Y

    2 D O C U M E N TAT I O N • Google • Stack Overflow • Blogs • IRC
  24. 26.

    T H E S Y M F O N Y

    2 D O C U M E N TAT I O N • Something missing? Add it yourself! • https://github.com/symfony/symfony-docs
  25. 27.

    T H E S Y M F O N Y

    2 D O C U M E N TAT I O N
  26. 28.

    P R O J E C T C O N

    F I G U R AT I O N
  27. 29.

    P R O J E C T C O N

    F I G U R AT I O N • Everything in it’s right place • config*.yml • routing*.yml • security.yml • parameters.yml
  28. 30.

    S E R V I C E C O N

    F I G U R AT I O N
  29. 31.

    S E R V I C E C O N

    F I G U R AT I O N • Yaml vs XML • Little detail: http://gowat.ch/xmlyml • http://converter.rosstuck.com/
  30. 32.

    C H O O S E A N D S

    TA N D A R D I S E
  31. 33.

    C H O O S E A N D S

    TA N D A R D I S E • You can do things in multiple ways • THIS IS GREAT! • Clarity, readability, maintainability
  32. 34.

    C H O O S E A N D S

    TA N D A R D I S E • Configuration: yml, xml, annotations • Controller extends or not? • Naming of services and parameters
  33. 37.

    C O M P O S E R • Commit

    composer.json and composer.lock • Run composer update <package> • Switch branch: composer install • Lock packages to version as much as possible • Lock packages to version as detailed as possible
  34. 38.

    C O M P O S E R THERE IS

    DRUSH INTEGRATION
  35. 40.
  36. 41.

    R E A D Y F O R A C

    T I O N ? • Forms: Always use the form component • Use bundles correctly • Maximize external bundle usage • PSR-0 • Avoid |raw
  37. 42.

    R E A D Y F O R A C

    T I O N ? • Change the session directory • Change the log directory • .gitignore your parameters.yml • Translate! • Use components if you don’t do full-stack
  38. 45.

    I S A L U T E Y O U

    leftontheweb.com php.ingewikkeld.net ! joind.in/12064 @skoop