Symfony Best Practices From The Trenches

Symfony 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

May 30, 2016
Tweet

Transcript

  1. None
  2. About me » PHPBenelux » PFZ » PHPAmersfoort/PHP.FRL » Ingewikkeld

    » phpBB, Zend Framework, Symfony and many more
  3. Contact @skoop & stefan@ingewikkeld.net

  4. Once upon a time...

  5. None
  6. Dependency Injection

  7. Dependency Injection » No hardcoded dependencies » Easily manage and

    update specific classes » Program to contracts, not implementations » Minimize bootstrap code » More testable code
  8. Dependency Injection class Foo { public function bar() { $coffee

    = new Coffee(); $coffee->init(); return $coffee->drink(); } }
  9. Dependency Injection $coffee = new Coffee();

  10. Dependency Injection $coffee->init();

  11. Dependency Injection class Coffee implements Roastable {} class Foo {

    private $coffee; public function __construct(Roastable $coffee) { $this->coffee = $coffee; } }
  12. Dependency Injection public function bar() { return $this->coffee->drink(); }

  13. Dependency Injection parameters: coffee.class: "Coffee" foo.class: "Foo" services: coffee: class:

    "%coffee.class%" foo: class: "%foo.class%" arguments: - "@coffee"
  14. Dependency Injection class DefaultController { public function fooAction() { $foo

    = $this->container->get('foo'); $foo->bar(); } }
  15. Dependency Injection class DefaultController { private $foo; public function __construct(Foo

    $foo) { $this->foo = $foo; } public function fooAction() { $this->foo->bar(); } }
  16. Service layer Or: How Symfony is only implementation

  17. Service layer » Seperation of concerns » Business logic should

    not be bound to the application » Service layer can be accessed through the service container
  18. Service layer public function showAction($productId) { $product = $this->getDoctrine() ->getRepository('AppBundle:Product')

    ->find($productId); if (!$product) { throw $this->createNotFoundException( 'No product found for id '.$productId ); } // ... do something, like pass the $product object into a template }
  19. None
  20. None
  21. Service Layer Hexagonal architecture » http://php-and-symfony.matthiasnoback.nl/tags/ hexagonal%20architecture/ » http://protalk.me/the-framework-as-an- implementation-detail

  22. None
  23. Documentation

  24. Documentation » The best starting point for your search »

    Not the ultimate source for information
  25. Documentation » Google » Stack Overflow » Blogs » IRC

  26. Documentation » Something missing? Add it yourself! » https://github.com/symfony/symfony-docs

  27. Documentation » Symfony Rainbow Series by Joshua Thijssen » https://leanpub.com/b/symfonyrainbowseries

  28. Project Configuration

  29. Project Configuration Everything in its right place » config*.yml »

    routing*.yml » security.yml » parameters.yml
  30. None
  31. Project Configuration XML vs yaml » http://gowat.ch/xmlyml » http://converter.rosstuck.com/

  32. Choose and Standardize

  33. Choose » You can do things in multiple ways »

    THIS IS GREAT! » Clarity, readability, maintainability
  34. Standardize » Configuration: yml, xml, annotations » Controller extends or

    not? » Naming of services, parameters, bundles
  35. Ready for action?

  36. Ready for action? » Use bundles correctly » Maximize external

    library usage » Avoid |raw » .gitignore your parameters.yml » Translate! » Log all the things! » Stay up-to-date
  37. Up-to-date » 2.7 - support until 05/2018, EOL 05/2019 »

    2.8 - support until 11/2018, EOL 11/2019 » 3.0 - support until 07/2016, EOL 01/2017 » Next LTS: 3.4: support until 11/2020, EOL 11/2021
  38. Questions?

  39. I Salute You

  40. I Salute You @skoop leftontheweb.com php.ingewikkeld.net

  41. I Salute You @skoop leftontheweb.com php.ingewikkeld.net Joind.in: http://gowat.ch/ sfbp