Symfony3 Best Practices from the trenches

Symfony3 Best Practices from the trenches

As done at PHP.FRL meetup in Heerenveen

A8f72e32766355f12a56ede9aaa0ee78?s=128

Stefan Koopmanschap

May 24, 2016
Tweet

Transcript

  1. 1.
  2. 2.

    About me » PHPBenelux » PFZ » PHPAmersfoort/PHP.FRL » Ingewikkeld

    » phpBB, Zend Framework, Symfony and many more
  3. 4.
  4. 6.

    Dependency Injection » No hardcoded dependencies » Easily manage and

    update specific classes » Program to contracts, not implementations » Minimize bootstrap code » More testable code
  5. 7.

    Dependency Injection class Foo { public function bar() { $coffee

    = new Coffee(); $coffee->init(); return $coffee->drink(); } }
  6. 10.

    Dependency Injection class Coffee implements Roastable {} class Foo {

    private $coffee; public function __construct(Roastable $coffee) { $this->coffee = $coffee; } }
  7. 12.

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

    "%coffee.class%" foo: class: "%foo.class%" arguments: - "@coffee"
  8. 13.
  9. 14.

    Dependency Injection class DefaultController { private $foo; public function __construct(Foo

    $foo) { $this->foo = $foo; } public function fooAction() { $this->foo->bar(); } }
  10. 16.

    Service layer » Seperation of concerns » Business logic should

    not be bound to the application » Service layer can be accessed through the service container
  11. 17.

    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 }
  12. 18.
  13. 19.
  14. 21.
  15. 23.

    Documentation » The best starting point for your search »

    Not the ultimate source for information
  16. 28.

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

    routing*.yml » security.yml » parameters.yml
  17. 29.
  18. 32.

    Choose » You can do things in multiple ways »

    THIS IS GREAT! » Clarity, readability, maintainability
  19. 33.
  20. 35.

    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
  21. 36.

    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