$30 off During Our Annual Pro Sale. View Details »

Symfony2 best practices from the trenches (ZendCon 2016)

Symfony2 best practices from the trenches (ZendCon 2016)

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'll 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's 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

October 20, 2016
Tweet

More Decks by Stefan Koopmanschap

Other Decks in Technology

Transcript

  1. View Slide

  2. ABOUT ME
    > PHPBenelux
    > PFZ
    > PHPAmersfoort/PHP.FRL
    > Ingewikkeld
    > phpBB, Zend Framework, Symfony and many more

    View Slide

  3. CONTACT
    @SKOOP
    &
    [email protected]

    View Slide

  4. ONCE UPON A
    TIME...

    View Slide

  5. View Slide

  6. DEPENDENCY
    INJECTION

    View Slide

  7. DEPENDENCY INJECTION
    > No hardcoded dependencies
    > Easily manage and update specific classes
    > Program to contracts, not implementations
    > Minimize bootstrap code
    > More testable code

    View Slide

  8. DEPENDENCY INJECTION
    class Foo
    {
    public function bar()
    {
    $coffee = new Coffee();
    $coffee->init();
    return $coffee->drink();
    }
    }

    View Slide

  9. DEPENDENCY INJECTION
    $coffee = new Coffee();

    View Slide

  10. DEPENDENCY INJECTION
    $coffee->init();

    View Slide

  11. DEPENDENCY INJECTION
    class Coffee implements Roastable {}
    class Foo
    {
    private $coffee;
    public function __construct(Roastable $coffee)
    {
    $this->coffee = $coffee;
    }
    }

    View Slide

  12. DEPENDENCY INJECTION
    public function bar()
    {
    return $this->coffee->drink();
    }

    View Slide

  13. DEPENDENCY INJECTION
    parameters:
    coffee.class: "Coffee"
    foo.class: "Foo"
    services:
    coffee:
    class: "%coffee.class%"
    foo:
    class: "%foo.class%"
    arguments:
    - "@coffee"

    View Slide

  14. DEPENDENCY INJECTION
    class DefaultController
    {
    public function fooAction()
    {
    $foo = $this->container->get('foo');
    $foo->bar();
    }
    }

    View Slide

  15. DEPENDENCY INJECTION
    class DefaultController
    {
    private $foo;
    public function __construct(Foo $foo)
    {
    $this->foo = $foo;
    }
    public function fooAction()
    {
    $this->foo->bar();
    }
    }

    View Slide

  16. SERVICE LAYER
    OR: HOW SYMFONY
    IS ONLY
    IMPLEMENTATION

    View Slide

  17. SERVICE LAYER
    > Seperation of concerns
    > Business logic should not be bound to the application
    > Service layer can be accessed through the service
    container

    View Slide

  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
    }

    View Slide

  19. View Slide

  20. View Slide

  21. SERVICE LAYER
    HEXAGONAL ARCHITECTURE
    > http://php-and-symfony.matthiasnoback.nl/tags/
    hexagonal%20architecture/
    > http://protalk.me/the-framework-as-an-
    implementation-detail

    View Slide

  22. View Slide

  23. DOCUMENTATION

    View Slide

  24. DOCUMENTATION
    > The best starting point for your search
    > Not the ultimate source for information

    View Slide

  25. DOCUMENTATION
    > Google
    > Stack Overflow
    > Blogs
    > IRC

    View Slide

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

    View Slide

  27. DOCUMENTATION
    > Symfony Rainbow Series by Joshua Thijssen
    > https://leanpub.com/b/symfonyrainbowseries

    View Slide

  28. PROJECT
    CONFIGURATION

    View Slide

  29. PROJECT CONFIGURATION
    EVERYTHING IN ITS RIGHT PLACE
    > config*.yml
    > routing*.yml
    > security.yml
    > parameters.yml

    View Slide

  30. View Slide

  31. PROJECT CONFIGURATION
    XML VS YAML
    > http://gowat.ch/xmlyml
    > http://converter.rosstuck.com/

    View Slide

  32. CHOOSE AND
    STANDARDIZE

    View Slide

  33. CHOOSE
    > You can do things in multiple ways
    > THIS IS GREAT!
    > Clarity, readability, maintainability

    View Slide

  34. STANDARDIZE
    > Configuration: yml, xml, annotations
    > Controller extends or not?
    > Naming of services, parameters, bundles

    View Slide

  35. PERSISTENCE

    View Slide

  36. READY FOR
    ACTION?

    View Slide

  37. 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

    View Slide

  38. 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
    > 3.1 - support until 01/2017, EOL 07/2017
    > 3.2 - support until 07/2017, EOL 01/2018
    > Next LTS: 3.4: support until 11/2020, EOL 11/2021

    View Slide

  39. QUESTIONS?

    View Slide

  40. I SALUTE YOU

    View Slide

  41. I SALUTE YOU
    @SKOOP
    LEFTONTHEWEB.COM
    PHP.INGEWIKKELD.NET

    View Slide

  42. I SALUTE YOU
    @SKOOP
    LEFTONTHEWEB.COM
    PHP.INGEWIKKELD.NET
    JOIND.IN: HTTPS://LEGACY.JOIND.IN/19492

    View Slide