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

Symfony2 Best Practices from the Trenches (PHPBenelux March Meetup, Valkenswaard)

Symfony2 Best Practices from the Trenches (PHPBenelux March Meetup, Valkenswaard)

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.

Stefan Koopmanschap

March 11, 2014
Tweet

More Decks by Stefan Koopmanschap

Other Decks in Programming

Transcript

  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. 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. 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. O U R T O P I C S F

    O R T O D AY • Choose and standardise • Composer • What else?
  5. D E P E N D E N C Y

    I N J E C T I O N
  6. 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
  7. 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(); } }
  8. 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(); } }
  9. 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. 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. 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; } }
  12. 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(); } }
  13. 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>
  14. 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(); } }
  15. 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
  16. 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
  17. 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
  18. S E R V I C E L AY E

    R Take it even further: ! http://protalk.me/the-framework-as-an- implementation-detail
  19. T H E S Y M F O N Y

    2 D O C U M E N TAT I O N
  20. 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
  21. 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
  22. 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
  23. T H E S Y M F O N Y

    2 D O C U M E N TAT I O N
  24. P R O J E C T C O N

    F I G U R AT I O N
  25. 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
  26. S E R V I C E C O N

    F I G U R AT I O N
  27. 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
  28. C H O O S E A N D S

    TA N D A R D I S E
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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!
  34. I S A L U T E Y O U

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