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

Get Started, Move Fast & Break things in Symfony2

weaverryan
February 08, 2014

Get Started, Move Fast & Break things in Symfony2

Grab some coffee and let's get your project moving on Symfony2! Since its 2.0 release in 2011, a lot of fantastic tools and best-practices have been built up around Symfony. In this talk, we'll start a real project and learn about some of these tools that can make us dangerous as quickly as possible! Symfony2 lets you write beautiful, maintainable code, and I'll give you 5 tips to do just that while staying aggressive with getting features out the door. RAD!

weaverryan

February 08, 2014
Tweet

More Decks by weaverryan

Other Decks in Technology

Transcript

  1. PHP Tutorial Screencasts
    Get Started, Move Fast
    & Break things in
    Symfony2

    View Slide

  2. Who is this beach bum?
    • The Symfony “Docs” guy
    !
    • KnpLabs US - Symfony consulting, training, Kumbaya
    !
    • Writer for KnpUniversity.com
    screencasts
    knplabs.com
    github.com/weaverryan
    PHP Tutorial Screencasts

    View Slide

  3. Success!

    View Slide

  4. Success?

    View Slide

  5. How do we define it?

    View Slide

  6. http://www.flickr.com/photos/martin_heigan/4544138976/
    A
    I delivered the Feature on time?

    View Slide

  7. I came in under budget?
    B
    http://www.flickr.com/photos/68751915@N05/6355360253/

    View Slide

  8. I delivered exactly what the
    client asked for?
    C
    http://www.flickr.com/photos/adesigna/4090782772/

    View Slide

  9. I delivered exactly what the
    client asked for?
    needed
    C
    http://www.flickr.com/photos/adesigna/4090782772/

    View Slide

  10. D
    The app has XX% test
    coverage?
    http://www.flickr.com/photos/sidelong/246816211/

    View Slide

  11. E
    My controllers average
    6.35 lines?
    http://www.flickr.com/photos/dharder9475/5381826270/

    View Slide

  12. F
    I use Symfony2 with
    55 Bundles!
    http://www.flickr.com/photos/sallypics/60614323/

    View Slide

  13. Which one is it?

    View Slide

  14. Define good…
    - BDD/TDD is *good*, right?
    - Skinny controllers are definitely
    good… I think?
    - 80%+ test coverage - is that good?

    View Slide

  15. We measure success
    entirely subjectively

    View Slide

  16. thus…

    View Slide

  17. Best practices & tools for
    “success” are
    subjectively decided

    View Slide

  18. We can aim for better!

    View Slide

  19. Measuring Success
    X*Quality - (1-X)*Speed = Success Score
    !
    where X is 0-1, related to many factors
    X = the Quality factor

    View Slide

  20. 1. Uncertainty
    2. Mission Critical Features
    3. Budget
    X-Factors

    View Slide

  21. 1. How much uncertainty does the project
    have?
    2. Are you building what you *know* will be
    successful, or is it more of an experiment?
    3. High Uncertainty => Low X (less quality,
    faster!)
    Uncertainty

    View Slide

  22. 1. Are you protecting bank
    data or patient records?
    2. Or are you selling
    Halloween costumes for
    hamsters?
    3. Mission Critical Features
    => High X (high quality,
    slower!)
    Mission Critical Features
    http://www.etsy.com/listing/111520104/superman-hamster-costume-hamster-pet

    View Slide

  23. 1. No budget? No choice, fast!
    2. No budget => low X (less quality,
    faster!)
    Budget

    View Slide

  24. Let’s build *success*

    View Slide

  25. Success:
    !
    Find what it means for
    *your* project and hit it!

    View Slide

  26. Writing a lot of tests
    may mean lower success

    View Slide

  27. Or it may
    save yer arse

    View Slide

  28. Or it may
    save yer arse
    @grmpyprogrammer
    … from him

    View Slide

  29. (Technical) Debt is not bad

    View Slide

  30. try buying a house
    without debt

    View Slide

  31. Manage and leverage
    technical debt

    View Slide

  32. RAD in Symfony2

    View Slide

  33. Symfony2 lends naturally
    to high quality apps
    (high X)

    View Slide

  34. How can we go fast?
    http://www.flickr.com/photos/curtisperry/141526923/

    View Slide

  35. 1. Shorten all the things
    2. Routing
    3. Querying for data
    4. Rendering the
    template
    5. Fixtures
    Speed up
    6. Forms
    7. Form theming
    8. Service config
    9. Security
    10. Creating “special”
    services

    View Slide

  36. 1
    http://hyperboleandahalf.blogspot.com/2010/06/this-is-why-ill-never-be-adult.html
    Shorten all the Things

    View Slide

  37. A bundle called “App”

    View Slide

  38. Brought to you by:

    View Slide

  39. 1. Allows you to have a bundle called simply
    “App”
    2. Shortens many things
    3. KnpRadBundle has a philosophy (optional)
    of only 1 bundle per project
    KnpRadBundle “App”

    View Slide

  40. a
    2
    Routing
    http://www.flickr.com/photos/14617207@N00/4471342864/

    View Slide

  41. A Routing File
    Bundle name Controller

    View Slide

  42. router:debug

    View Slide

  43. 1. Automatic CRUD routes
    2. Ability to remove/add more
    3. FOSRestBundle does this well for REST
    controllers
    4. SensioFrameworkExtraBundle allows for
    annotation routing
    KnpRadBundle Routing

    View Slide

  44. View Slide

  45. 3
    Querying for Data

    View Slide

  46. Automatically Queried

    View Slide

  47. !
    1. Normally configured via a ParamConverter
    annotation
    2. Optional if:
    A. You type-hint the argument
    B. The routing wildcard is “id” or matches
    the argument name
    SensioFrameworkExtraBundle

    View Slide

  48. View Slide

  49. A Routing File
    teams is available as an argument
    to any method in the controller

    View Slide

  50. 1. SensioFrameworkExtraBundle doesn’t
    work to query for an array of objects
    2. “teams” variable can be used across all
    actions
    KnpRadBundle Resources

    View Slide

  51. 4
    Rendering the Template

    View Slide

  52. Will Render App:Team:show.html.twig
    App\Controller\TeamController::showAction

    View Slide

  53. But wait, the template

    doesn’t exist yet!!!

    View Slide

  54. View Slide

  55. 1. Finds the template and renders it
    2. Creates it if it doesn’t exist
    3. SensioFrameworkExtraBundle also does
    this, but requires an annotation
    KnpRadBundle

    View Slide

  56. 5
    Fixtures!
    http://www.flickr.com/photos/ciordia/8162313/

    View Slide

  57. AppBundle/Resources/fixtures/fixtures.yml

    View Slide

  58. Brought to you by:
    Alice & Faker
    https://github.com/nelmio/alice
    https://github.com/fzaninotto/Faker

    View Slide

  59. AppBundle/Resources/fixtures/fixtures.yml
    Create 50 users

    View Slide

  60. AppBundle/Resources/fixtures/fixtures.yml
    Create 50 users
    Create 50 users
    Faker gives you fake

    data functions

    View Slide

  61. AppBundle/Resources/fixtures/fixtures.yml
    Create 50 users
    Create 50 users
    Associate with a

    random team
    Faker gives you fake

    data functions

    View Slide

  62. Load any Resources/fixtures/*.yml file

    easily with DoctrineFixturesBundle

    View Slide

  63. View Slide

  64. 6
    Forms!
    http://www.flickr.com/photos/theworldisatwarwegonnastopit/4443672898/

    View Slide

  65. View Slide

  66. Creates and

    binds the form

    View Slide

  67. Creates and

    binds the form
    Creates and

    binds the form
    Shortcut methods

    on KnpRadBundle

    base Controller

    View Slide

  68. 1. Form is created automatically for you
    based on the public getters/setters OR it
    finds a {ClassName}Type and uses it
    2. Form is automatically bound (if PUT/
    POST)
    KnpRadBundle Form Shortcuts

    View Slide

  69. 7
    Form Theming!

    View Slide

  70. 1. Uses custom form themes for Twitter
    Bootstrap
    2. Install and it works
    3. Comes with way too many other features,
    but can easily be used for *just* the form
    theming
    MopaBootstraBundle

    View Slide

  71. 8
    Service Configuration!

    View Slide

  72. 1. Normally done with a services.yml or
    services.xml
    2. KnpRadBundle automatically loads a
    Resources/config/services.yml
    Service Configuration

    View Slide

  73. JMSDiExtraBundle allows
    annotation configuration

    View Slide

  74. View Slide

  75. 1. JmsDiExtraBundle saves you a little bit of
    time, but I don’t love it
    2. Seems to have cache issues
    JmsDiExtraBundle?

    View Slide

  76. Security
    9
    http://www.flickr.com/photos/carbonnyc/2294144289/

    View Slide

  77. View Slide

  78. Check for user roles

    View Slide

  79. Check for user roles
    Custom object
    permissions
    (with voters)

    View Slide

  80. SensioFrameworkExtraBundle
    !
    1. Flexible by using voters to hold complex
    authorization logic
    2. Easily add authorization and scan for it later

    View Slide

  81. Special Services
    10

    View Slide

  82. Certain classes are auto-registered into
    the service container by convention
    KnpRadBundle

    View Slide

  83. What? Dir
    Doctrine Repo n/a
    Twig Extension Twig/
    Form Type Form/
    Form Type Extension Form/Extension/
    Validation Constraint Validator/Constraints/
    Security Voter Security/
    Auto-Service Registration

    View Slide

  84. Using the auto-registered
    repository service

    View Slide

  85. Auto-Service Registration
    1. Implement ContainerAwareInterface to be
    passed the container
    2. Offload logic to another service to avoid bad
    practice of using the injected container

    View Slide

  86. Part 3:
    !
    Iterating Quickly

    View Slide

  87. Lets learn from our
    experiments quickly
    and adjust

    View Slide

  88. 1 Error Notification
    1. Be ok with errors, but *know* about them
    2. Get emailed on errors (or something
    equivalent)*
    3. Use NewRelic
    * http://symfony.com/doc/current/cookbook/logging/monolog_email.html

    View Slide

  89. 2 Preventing or Fixing Errors
    1. Code safely, but not too safely
    2. Respond to a bug with effort that is
    proportional to the pain it caused you
    3. … so don’t freak out about every
    complaint

    View Slide

  90. 3 Behat
    Behat converts human-readable 

    descriptions of a feature into a 

    functional test

    View Slide

  91. Behat team.feature file

    View Slide

  92. View Slide

  93. 3 Behat
    1. You already need to think about the
    behavior of a feature
    2. Fast to write, especially “smoke screen”
    testing
    3. Confidence that no core *behavior* is
    broken on each depot

    View Slide

  94. 4 CI / Travis
    1. Develop quickly without worrying about
    tests
    2. Always know if your app is in a
    deployable state

    View Slide

  95. 5 PHPStorm
    1. Community-developed “Symfony”
    plugin
    2. Autocompletion for service
    names, route names, template
    names, Twig functions, methods
    on services
    3. Click directly into templates,
    classes behind a service

    View Slide

  96. It’s basically ridiculous

    View Slide

  97. 1. RADBundle developer
    2. Professional nice guy
    3. Helped with this
    presentation
    6 Tips from Florian
    @docteur_klein

    View Slide

  98. 6 Abstraction
    @docteur_klein
    Florian says:
    Don’t over engineer with

    abstraction layers

    (YAGNI)

    View Slide

  99. Maybe you need Silex

    not Symfony

    View Slide

  100. 6 Small Classes
    @docteur_klein
    Florian says:
    Create many small classes

    (single responsibility
    principle)

    View Slide

  101. 1. Library given you trouble? Based on the quality
    factor, consider abandoning it
    2. KnpRADBundle has *many* features, not all are
    documented or are even a great idea. Use what
    works, move on quickly.
    7 Abandon problems

    View Slide

  102. Final Act
    !
    Break (some) things

    View Slide

  103. 1. Define your success metric
    A. How much uncertainty?
    B. Mission critical features
    C. Did we find any money?
    RAD

    View Slide

  104. 2. Choose your RAD Features
    • KnpRadBundle
    • SensioFrameworkExtraBundle
    • MopaBootstrapBundle
    • Alice+Faker
    • SonataAdminBundle
    • APYDataGridBundle
    • …
    RAD

    View Slide

  105. 3. Iterate Quickly and with Confidence
    • Error notification
    • Behat
    • CI / Travis
    • PHPStorm + Symfony Plugin
    • Avoid over-abstraction
    • Abandon failing solutions (quickly, but
    not too quickly)
    RAD

    View Slide

  106. Learn Faster

    View Slide

  107. Make a more *successful* project

    View Slide

  108. https://joind.in/10509
    PHP Tutorial Screencasts
    Ryan Weaver
    @weaverryan

    View Slide

  109. @weaveryan
    @KnpUniversity
    https://joind.in/10509
    https://github.com/knpuniversity/sunshinephp-rad

    View Slide