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

The Great Framework Kumbaya

weaverryan
October 31, 2014

The Great Framework Kumbaya

Time to gather our frameworks around a warm camp fire and see just how much we have in common In this presentation, we take a flat PHP4-style application and gently migrate it into our own "framework", that uses components from Symfony2, Zend Framework 2, Aura and a library called Pimple. By the end, you'll see how any ugly application can take advantage of the many wonderful tools available to PHP developers. You'll also learn exactly how *any* framework actually works under the hood by hooking up routing, controllers, services and events.

weaverryan

October 31, 2014
Tweet

More Decks by weaverryan

Other Decks in Technology

Transcript

  1. by your friend:
    !
    Ryan Weaver
    @weaverryan
    The Great
    Framework Kumbaya

    View Slide

  2. KnpUniversity.com

    github.com/weaverryan
    Who is this Happy Camper?
    > Lead for the Symfony documentation

    > KnpLabs US - Symfony Consulting,

    training, Kumbaya

    !
    > Writer for KnpUniversity.com Tutorials
    > Husband of the much more

    talented @leannapelham

    View Slide

  3. !
    Personal
    Homepage

    View Slide

  4. View Slide

  5. View Slide

  6. Yep, that’s a

    sausage hat
    wink wink

    that’s me!
    @jmikola

    View Slide

  7. http://bit.ly/php-camp
    @weaverryan

    View Slide

  8. Open our Database connection
    // index.php

    View Slide

  9. Try to get a clean URI
    // index.php

    View Slide

  10. Render the homepage
    // index.php

    View Slide

  11. List our happy campers!
    // index.php

    View Slide

  12. Why don’t we code

    like this anymore?

    View Slide

  13. The Tribes of PHP
    meet the UN

    View Slide

  14. Formerly Solar

    View Slide

  15. > The United Nations of PHP

    !
    > Equally Important

    !
    > Equally dysfunctional (like any happy family)
    @weaverryan

    View Slide

  16. View Slide

  17. FIG != collaboration

    View Slide

  18. FIG Empowers You…

    View Slide

  19. To become
    best friends
    with all the
    frameworks

    View Slide

  20. A) Reading request info could be better

    !
    B) can’t set headers later on

    !
    C) front controller

    !
    D) little URL control

    !
    E) all objects are global

    !
    F) 1 giant file (no separation)

    !
    G) HTML and PHP is mixed!
    Problems
    @weaverryan

    View Slide

  21. !
    $_SERVER[‘HOST']
    !
    header('X-POWERED-BY: crap');

    View Slide

  22. View Slide

  23. View Slide

  24. composer require symfony/http-foundation
    … used by Laravel, Silex, Symfony, Drupal 8…

    View Slide

  25. Current Status
    @weaverryan

    View Slide

  26. WOW!

    View Slide

  27. ... becomes ...
    The “Request” object

    View Slide

  28. ... becomes ...
    The “Request” object

    View Slide

  29. ... becomes ...
    The “Response” object

    View Slide

  30. !
    if ($url == '/') {

    View Slide

  31. if ($uri == '/' || $uri == '') {
    // ...
    } elseif ($uri == '/attendees') {
    // ...
    } else {
    // ...
    }
    @weaverryan
    Our app is a giant gross “if” statement
    Problems
    Grabbing a piece from the URL like

    /blog/my-blog-post will take some work

    View Slide

  32. Aura.Router

    View Slide

  33. composer require aura/router
    First composer require

    View Slide

  34. Then Celebrate!

    View Slide

  35. @weaverryan
    Current Status

    View Slide

  36. 1) Map URI to “controller”

    View Slide

  37. View Slide

  38. View Slide

  39. View Slide

  40. 2) Execute the controller*
    * each controller is a flat function

    View Slide

  41. The Controllers

    View Slide

  42. $kitten--

    View Slide

  43. 1. Request cleans the URI

    2. Router matches the URI to a route, returns a 

    “controller” string

    3. We execute the controller function

    4. The controller creates a Response object

    5. We send the Response headers and content
    The Big Picture
    @weaverryan

    View Slide

  44. our 20 line framework
    solves…

    View Slide

  45. A) Reading request info could be better

    !
    B) can’t set headers later on

    !
    C) front controller

    !
    D) little URL control

    !
    E) all objects are global

    !
    F) 1 giant file (no separation)

    !
    G) HTML and PHP is mixed!
    Problems
    @weaverryan

    View Slide

  46. !
    Global objects
    are universally hated

    View Slide

  47. > We’ve got lots of random, disorganized

    objects floating around and some depend

    on others
    Problems
    > And we can’t easily access them from

    within our controllers
    @weaverryan

    View Slide

  48. Pimple!
    … used by Silex

    … name loved by ???

    View Slide

  49. composer require pimple/pimple
    First composer require

    View Slide

  50. Then Celebrate!

    View Slide

  51. Centralize the db connection

    View Slide

  52. Centralize the db connection

    View Slide

  53. Pass the container
    to the controller

    View Slide

  54. $kitten++

    View Slide

  55. Container all the things!

    View Slide

  56. View Slide

  57. View Slide

  58. With everything in the
    container, our “framework”
    just got skinny

    View Slide

  59. View Slide

  60. View Slide

  61. !
    Tackling
    Disorganization

    View Slide

  62. We have 1 file with everything mixed

    !
    !
    1) Creation of container & objects

    2) Definition of routes

    3) Definition of controllers

    4) The “framework” code that executes

    everything

    Problems
    @weaverryan

    View Slide

  63. We have 1 file with everything mixed

    !
    1) services.php

    2) routes.php

    3) controllers.php

    4) bootstrap.php (framework code)

    Basic Solution
    @weaverryan

    View Slide

  64. index.php
    Nothing to see here...

    View Slide

  65. bootstrap.php

    View Slide

  66. services.php

    View Slide

  67. routing.php

    View Slide

  68. controllers.php

    View Slide

  69. bootstrap.php
    … all the same “stuff”…

    View Slide

  70. All Frameworks Work Like This

    View Slide

  71. View Slide

  72. View Slide

  73. !

    View Slide

  74. Problems
    @weaverryan
    bleh!

    View Slide

  75. composer require twig/twig
    First composer require

    View Slide

  76. Then Celebrate!

    View Slide

  77. Before

    View Slide

  78. After

    View Slide

  79. views/attendees.twig

    View Slide

  80. Getting kinda easy, right?

    View Slide

  81. What’s available?
    Search GitHub!

    View Slide

  82. All Your Frameworks
    Are Belong to Us

    View Slide

  83. To focus on the

    interesting problems…

    !
    (business problems)

    View Slide

  84. … we need to remove the
    boring, repetitive problems…

    View Slide

  85. … but don’t limit yourself
    to one “world”…
    Symfony Developer
    Laravel Developer
    Drupal Developer
    Silex Developer

    View Slide

  86. … be a PHP Developer

    View Slide

  87. Thanks!
    Ryan Weaver

    @weaverryan
    KnpUniversity.com
    Symfony, Behat, Twig, OO, etc Tutorial Screencasts
    https://joind.in/11898

    View Slide