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

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. 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. TRYING TO PLAY http://joind.in/12064

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

    O R T O D AY • Choose and standardise • Composer • What else?
  6. O N C E U P O N A T

    I M E …
  7. O N C E U P O N A T

    I M E …
  8. D E P E N D E N C Y

    I N J E C T I O N
  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
  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 Foo { public function bar() { $coffee = new Coffee(); $coffee->init(); return $coffee->drink(); } }
  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(); } }
  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(); } }
  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; } }
  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(); } }
  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>
  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(); } }
  18. S E R V I C E L AY E

    R
  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
  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
  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
  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
  23. T H E S Y M F O N Y

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

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

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

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

    TA N D A R D I S E
  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
  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
  35. C O M P O S E R

  36. C O M P O S E R ALWAYS USE

    COMPOSER
  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
  38. C O M P O S E R THERE IS

    DRUSH INTEGRATION
  39. C O M P O S E R ALWAYS USE

    COMPOSER
  40. R E A D Y F O R A C

    T I O N ?
  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
  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
  43. Q U E S T I O N S ?

  44. I S A L U T E Y O U

  45. I S A L U T E Y O U

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