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

Going bare - writing the web without a framework - php[world] 2018

Going bare - writing the web without a framework - php[world] 2018

This talk was given at php[world] 2018

If you've only ever experienced web development through a web framework, you're missing out. Have you ever been tempted to rewrite a codebase in your favorite framework? That's a smell that you need to come to this talk. Being comfortable developing without a framework is a crucial skill to have for refactoring legacy applications. In this talk I show you how to harness the power of going "frameworkless" so you'll feel empowered to make better decisions in your next web project. Be set free from your web-framework ball and chain.

Sammy Kaye Powers

November 15, 2018
Tweet

More Decks by Sammy Kaye Powers

Other Decks in Programming

Transcript

  1. Going Bare
    Writing the web
    without a framework
    Sammy Kaye Powers
    2018-11-15

    View Slide

  2. Thanks to Our
    Sponsors
    2018

    View Slide

  3. joind.in/talk/c3f16
    @SammyK #phpworld
    Slides
    Get the
    joind.in/talk/c3f16

    View Slide

  4. joind.in/talk/c3f16
    @SammyK #phpworld
    frameworks
    The point is
    not
    <3 frameworks!
    I

    View Slide

  5. joind.in/talk/c3f16
    @SammyK #phpworld
    opinion
    It’s OK to have an

    View Slide

  6. joind.in/talk/c3f16
    @SammyK #phpworld
    experiment
    It’s OK to

    View Slide

  7. joind.in/talk/c3f16
    @SammyK #phpworld
    Bootstrapping
    / ˈbo ͞
    otˌstraˌpiNG /
    Define:
    (…at least for this talk)
    Code that acts as
    an entry point
    into an app.

    View Slide

  8. joind.in/talk/c3f16
    @SammyK #phpworld
    Boilerplate
    / ˈboilərˌplāt /
    Define:
    The “glue” code that
    ties components
    together.
    (…at least for this talk)

    View Slide

  9. joind.in/talk/c3f16
    @SammyK #phpworld
    Frameworks are a
    tool

    View Slide

  10. joind.in/talk/c3f16
    @SammyK #phpworld
    Going bare is a
    skill
    (that you don’t get with a framework)

    View Slide

  11. joind.in/talk/c3f16
    @SammyK #phpworld
    The of going bare
    skill
    • Writing bootstrap & boilerplate
    • Choosing exact components
    • Use different paradigms
    • Use those skills on domain code
    (Get to know the component ecosystem)
    (Like singleton, et al)
    (Gives you more control over how components interact)
    (Makes you better at writing your domain code)

    View Slide

  12. joind.in/talk/c3f16
    @SammyK #phpworld
    Myths
    about
    going
    frameworkless

    View Slide

  13. joind.in/talk/c3f16
    @SammyK #phpworld
    Myth #1
    You must write
    everything from
    scratch

    View Slide

  14. joind.in/talk/c3f16
    @SammyK #phpworld
    Myth #2
    You’re inherently
    anti-framework or
    anti-convention

    View Slide

  15. joind.in/talk/c3f16
    @SammyK #phpworld
    Myth #3
    You’ll spend most of
    your time on
    boilerplate instead of
    on domain code

    View Slide

  16. joind.in/talk/c3f16
    @SammyK #phpworld
    Myth #4
    You must be an
    architecture
    astronaut

    View Slide

  17. joind.in/talk/c3f16
    @SammyK #phpworld
    Myth #5
    You like pain

    View Slide

  18. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros
    Framework
    Cons

    View Slide

  19. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros

    View Slide

  20. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros

    View Slide

  21. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros
    crazy
    fast
    You get started

    View Slide

  22. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros
    First-time
    install time
    minutes!
    5

    View Slide

  23. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros

    View Slide

  24. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros
    boilerplate
    all the things
    You don’t have to Con
    but it can be a

    View Slide

  25. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros
    bootstrap &
    boilerplate
    code
    You don’t have to maintain

    View Slide

  26. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros
    battle-tested
    The code is

    View Slide

  27. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros
    conventions
    Ready-to-follow
    Con
    but it can be a

    View Slide

  28. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros
    documentation
    Most frameworks
    have great

    View Slide

  29. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros
    marketable
    It makes you more

    View Slide

  30. joind.in/talk/c3f16
    @SammyK #phpworld
    Pros
    pragmatic
    They are often

    View Slide

  31. joind.in/talk/c3f16
    @SammyK #phpworld
    cons?
    What about the

    View Slide

  32. joind.in/talk/c3f16
    @SammyK #phpworld
    Cons
    legacy
    codebases
    Useless for

    View Slide

  33. joind.in/talk/c3f16
    @SammyK #phpworld
    Cons
    cruft
    They fill your project with

    View Slide

  34. joind.in/talk/c3f16
    @SammyK #phpworld
    Cons
    Cruft
    • Deployment takes longer
    • Upgrading pulls in all
    the new features

    View Slide

  35. joind.in/talk/c3f16
    @SammyK #phpworld
    Cons
    tightly couple
    Easier to
    due to convenience

    View Slide

  36. joind.in/talk/c3f16
    @SammyK #phpworld

    View Slide

  37. 5.3

    View Slide

  38. 5.7
    2018-09-04

    View Slide

  39. View Slide

  40. 5.7
    Did not
    support
    laravelcollective/html
    mccool/laravel-auto-presenter

    View Slide

  41. View Slide

  42. View Slide

  43. joind.in/talk/c3f16
    @SammyK #phpworld
    Cons
    upgrading
    Framework-specific deps
    can keep you from

    View Slide

  44. joind.in/talk/c3f16
    @SammyK #phpworld
    The
    The Cons
    Pros seem great
    are weak…

    View Slide

  45. joind.in/talk/c3f16
    @SammyK #phpworld
    Why
    would you go bare?
    (To answer we must ask…)

    View Slide

  46. joind.in/talk/c3f16
    @SammyK #phpworld
    When
    should you go bare?

    View Slide

  47. joind.in/talk/c3f16
    @SammyK #phpworld

    View Slide

  48. joind.in/talk/c3f16
    @SammyK #phpworld
    Greenfield Legacy
    Depends

    View Slide

  49. joind.in/talk/c3f16
    @SammyK #phpworld
    1%
    99%
    90%
    10%
    Go Bare Framwork
    Greenfield Legacy
    Going bare vs framework

    View Slide

  50. joind.in/talk/c3f16
    @SammyK #phpworld
    How
    do you go bare?

    View Slide

  51. joind.in/talk/c3f16
    @SammyK #phpworld
    from
    scratch
    You could just write

    View Slide

  52. joind.in/talk/c3f16
    @SammyK #phpworld

    View Slide

  53. joind.in/talk/c3f16
    @SammyK #phpworld
    That means writing all the things
    Router DBAL/ORM Dep Injection Logger

    View Slide

  54. joind.in/talk/c3f16
    @SammyK #phpworld
    reinventing
    the wheel
    But that’s like

    View Slide

  55. joind.in/talk/c3f16
    @SammyK #phpworld
    Better Way™
    There’s a

    View Slide

  56. joind.in/talk/c3f16
    @SammyK #phpworld
    po-
    nents
    Com-

    View Slide

  57. joind.in/talk/c3f16
    @SammyK #phpworld
    components
    has a healthy ecosystem of

    View Slide

  58. joind.in/talk/c3f16
    @SammyK #phpworld
    using them
    You’re probably already

    View Slide

  59. joind.in/talk/c3f16
    @SammyK #phpworld
    built on them
    Most frameworks are
    zend-router zend-db zend-di zend-log

    View Slide

  60. joind.in/talk/c3f16
    @SammyK #phpworld
    choose ur candy
    Going bare lets you
    Look at all
    those smarties!
    Yum!

    View Slide

  61. joind.in/talk/c3f16
    @SammyK #phpworld
    I like smarties

    View Slide

  62. joind.in/talk/c3f16
    @SammyK #phpworld
    Router DBAL/ORM Dep Injection Logger

    View Slide

  63. joind.in/talk/c3f16
    @SammyK #phpworld
    zend-router fast-route php-router phroute
    Routers
    Tons of variety

    View Slide

  64. joind.in/talk/c3f16
    @SammyK #phpworld
    Easily installed with

    View Slide

  65. joind.in/talk/c3f16
    @SammyK #phpworld
    bag o’ candy
    A framework is like a
    Comes with
    licorice?
    Yuck!

    View Slide

  66. joind.in/talk/c3f16
    @SammyK #phpworld
    I don’t like licorice

    View Slide

  67. joind.in/talk/c3f16
    @SammyK #phpworld
    components
    When you go bare
    don’t just magically start
    working together

    View Slide

  68. joind.in/talk/c3f16
    @SammyK #phpworld
    Key:
    Get comfy with
    bootstrapping &
    boilerplate code
    It’s really
    not that bad

    View Slide

  69. joind.in/talk/c3f16
    @SammyK #phpworld
    examples
    Real-world

    View Slide

  70. joind.in/talk/c3f16
    @SammyK #phpworld
    conventions
    When going bare, there aren’t any
    …so this is a way,
    not the way

    View Slide

  71. joind.in/talk/c3f16
    @SammyK #phpworld
    Greenfield
    Let’s start with

    View Slide

  72. joind.in/talk/c3f16
    @SammyK #phpworld
    Case study
    Raw
    CSV
    Calculate

    & save
    to DB
    Show
    report
    Client’s server Our server

    View Slide

  73. joind.in/talk/c3f16
    @SammyK #phpworld
    Case study
    Download CSV & calculate
    CLI
    View the report
    HTTP Endpoint

    View Slide

  74. joind.in/talk/c3f16
    @SammyK #phpworld
    coding domain
    in 5 mins
    Install framework &

    View Slide

  75. joind.in/talk/c3f16
    @SammyK #phpworld
    61components
    installed before
    we code
    https://framework.zend.com/downloads

    View Slide

  76. joind.in/talk/c3f16
    @SammyK #phpworld
    go micro
    We could…
    Slim Silex
    (et al)

    View Slide

  77. joind.in/talk/c3f16
    @SammyK #phpworld
    Silex
    DEAD!

    View Slide

  78. joind.in/talk/c3f16
    @SammyK #phpworld
    go bare! :)
    But let’s

    View Slide

  79. joind.in/talk/c3f16
    @SammyK #phpworld
    Start in the CLI
    (Just like when you use a framework!)

    View Slide

  80. joind.in/talk/c3f16
    @SammyK #phpworld
    1. Make an empty
    composer.json or…
    $ composer init

    View Slide

  81. joind.in/talk/c3f16
    @SammyK #phpworld
    2. Make a folder for
    domain code
    $ mkdir src

    View Slide

  82. joind.in/talk/c3f16
    @SammyK #phpworld
    3. Add autoloading
    $ composer \
    dumpautoload
    And then run…

    View Slide

  83. joind.in/talk/c3f16
    @SammyK #phpworld
    Now just install your
    components &
    tie them together

    View Slide

  84. joind.in/talk/c3f16
    @SammyK #phpworld
    Key:
    Get comfy with
    bootstrapping &
    boilerplate code
    It’s really
    not that bad

    View Slide

  85. joind.in/talk/c3f16
    @SammyK #phpworld
    Case study
    Download CSV & calculate
    CLI
    View the report
    HTTP Endpoint

    View Slide

  86. joind.in/talk/c3f16
    @SammyK #phpworld
    Install a
    console component
    $ composer require symfony/console
    (or maybe you like zendframework/zend-console)

    View Slide

  87. joind.in/talk/c3f16
    @SammyK #phpworld
    src/Console/Runner.php

    View Slide

  88. joind.in/talk/c3f16
    @SammyK #phpworld
    We get to make our
    first bootstrap code!

    View Slide

  89. joind.in/talk/c3f16
    @SammyK #phpworld
    run (sans .php extension)
    $ chmod +x run

    View Slide

  90. joind.in/talk/c3f16
    @SammyK #phpworld
    $ ./run

    View Slide

  91. joind.in/talk/c3f16
    @SammyK #phpworld
    B.A.U.
    Now it’s just
    src/Console/Runner.php

    View Slide

  92. joind.in/talk/c3f16
    @SammyK #phpworld
    src/Console/CalculateReportsCommand.php

    View Slide

  93. joind.in/talk/c3f16
    @SammyK #phpworld
    $ ./run

    View Slide

  94. joind.in/talk/c3f16
    @SammyK #phpworld

    View Slide

  95. joind.in/talk/c3f16
    @SammyK #phpworld
    Congrats!

    View Slide

  96. joind.in/talk/c3f16
    @SammyK #phpworld
    Install the
    DI container
    $ composer require pimple/pimple
    (Choose your poison)

    View Slide

  97. joind.in/talk/c3f16
    @SammyK #phpworld
    Need to
    make
    singleton
    ready
    src/Application.php

    View Slide

  98. joind.in/talk/c3f16
    @SammyK #phpworld
    Singleton
    Isn’t that an anti-pattern?

    View Slide

  99. joind.in/talk/c3f16
    @SammyK #phpworld
    https://en.wikipedia.org/wiki/Singleton_pattern

    View Slide

  100. joind.in/talk/c3f16
    @SammyK #phpworld

    View Slide

  101. joind.in/talk/c3f16
    @SammyK #phpworld
    Singleton
    •Ensures one & only one instance
    •Global access (static function)
    •Initialization on first access

    View Slide

  102. joind.in/talk/c3f16
    @SammyK #phpworld
    Fatal error: Call to private
    App\Application::__construct()
    src/Application.php

    View Slide

  103. joind.in/talk/c3f16
    @SammyK #phpworld
    src/Application.php
    Global access
    + instantiate
    on first use

    View Slide

  104. joind.in/talk/c3f16
    @SammyK #phpworld
    Get an instance of Application

    View Slide

  105. joind.in/talk/c3f16
    @SammyK #phpworld
    Prevent cloning
    src/Application.php

    View Slide

  106. joind.in/talk/c3f16
    @SammyK #phpworld
    src/Application.php
    Prevent unserialization

    View Slide

  107. joind.in/talk/c3f16
    @SammyK #phpworld
    •Ensures one & only one instance
    •Global access (static function)
    •Initialization on first access

    View Slide

  108. joind.in/talk/c3f16
    @SammyK #phpworld
    Case study
    Download CSV & calculate
    CLI
    View the report
    HTTP Endpoint

    View Slide

  109. joind.in/talk/c3f16
    @SammyK #phpworld
    We get to make our
    final bootstrap code!

    View Slide

  110. joind.in/talk/c3f16
    @SammyK #phpworld
    public/index.php

    View Slide

  111. joind.in/talk/c3f16
    @SammyK #phpworld
    Eh?

    View Slide

  112. joind.in/talk/c3f16
    @SammyK #phpworld
    src/Application.php
    However you
    register service
    providers into the
    container
    (different DI containers have their own API)

    View Slide

  113. joind.in/talk/c3f16
    @SammyK #phpworld
    Why not
    in constructor?

    View Slide

  114. joind.in/talk/c3f16
    @SammyK #phpworld
    src/Application.php

    View Slide

  115. joind.in/talk/c3f16
    @SammyK #phpworld
    Install
    a router
    $ composer require nikic/fast-route
    (Choose your poison)

    View Slide

  116. joind.in/talk/c3f16
    @SammyK #phpworld
    service provider
    Make a routing
    Pimple syntax - mileage my vary

    View Slide

  117. joind.in/talk/c3f16
    @SammyK #phpworld
    Register in container
    src/Application.php

    View Slide

  118. joind.in/talk/c3f16
    @SammyK #phpworld
    src/Router.php

    View Slide

  119. joind.in/talk/c3f16
    @SammyK #phpworld
    Ugly A.F.
    src/routes.php

    View Slide

  120. joind.in/talk/c3f16
    @SammyK #phpworld
    Different project,
    same component
    src/routes.php

    View Slide

  121. joind.in/talk/c3f16
    @SammyK #phpworld
    public/index.php
    run
    We got bootstrap
    Runs CLI Runs HTTP

    View Slide

  122. joind.in/talk/c3f16
    @SammyK #phpworld
    We got boilerplate
    src/Router.php
    src/Console/Runner.php
    symfony/console nikic/fast-route

    View Slide

  123. joind.in/talk/c3f16
    @SammyK #phpworld
    Still not
    convinced?

    View Slide

  124. joind.in/talk/c3f16
    @SammyK #phpworld
    What about
    Legacy

    View Slide

  125. joind.in/talk/c3f16
    @SammyK #phpworld
    Why
    would you go bare?
    It’s a skill you can use
    for legacy apps

    View Slide

  126. joind.in/talk/c3f16
    @SammyK #phpworld
    I’ll just rewrite this
    in x framework…

    View Slide

  127. joind.in/talk/c3f16
    @SammyK #phpworld
    Totes
    samezies
    except routing

    View Slide

  128. joind.in/talk/c3f16
    @SammyK #phpworld
    Route exists?
    Not found
    Router
    Yes No
    Code
    404

    View Slide

  129. joind.in/talk/c3f16
    @SammyK #phpworld
    Route exists?
    Legacy router
    New router
    Yes No
    New code

    View Slide

  130. joind.in/talk/c3f16
    @SammyK #phpworld
    app/webroot/index.php
    Old CakePHP bootstrap file

    View Slide

  131. joind.in/talk/c3f16
    @SammyK #phpworld
    src/bootstrap.php
    Same as public/index.php for HTTP

    View Slide

  132. joind.in/talk/c3f16
    @SammyK #phpworld
    src/Router.php

    View Slide

  133. joind.in/talk/c3f16
    @SammyK #phpworld
    tests?
    What about

    View Slide

  134. joind.in/talk/c3f16
    @SammyK #phpworld
    Install
    PHPUnit
    $ composer require phpunit/phpunit --dev

    View Slide

  135. joind.in/talk/c3f16
    @SammyK #phpworld
    phpunit.xml

    View Slide

  136. joind.in/talk/c3f16
    @SammyK #phpworld
    tests/bootstrap.php

    View Slide

  137. joind.in/talk/c3f16
    @SammyK #phpworld
    src/Application.php

    View Slide

  138. joind.in/talk/c3f16
    @SammyK #phpworld
    Run the
    test suite
    $ ./vendor/bin/phpunit

    View Slide

  139. joind.in/talk/c3f16
    @SammyK #phpworld
    How to choose your
    components

    View Slide

  140. joind.in/talk/c3f16
    @SammyK #phpworld
    Use more than one
    framework
    #1

    View Slide

  141. joind.in/talk/c3f16
    @SammyK #phpworld
    Search
    packagist.org
    #2
    (Make sure it has good docs &
    is actively maintained)

    View Slide

  142. joind.in/talk/c3f16
    @SammyK #phpworld
    experiments
    Try some

    View Slide

  143. joind.in/talk/c3f16
    @SammyK #phpworld
    100%
    middleware
    My next experiment:
    (I could totes use Zend Expressive or just go bare)

    View Slide

  144. joind.in/talk/c3f16
    @SammyK #phpworld
    pros?
    framework
    ‘member the

    View Slide

  145. joind.in/talk/c3f16
    @SammyK #phpworld
    Framework pros
    Crazy fast
    to get started
    …but you’ll get faster
    at going bare

    View Slide

  146. joind.in/talk/c3f16
    @SammyK #phpworld
    Framework pros
    to maintain
    …most the time it’s
    really not that bad
    No bootstrap & boilerplate code

    View Slide

  147. joind.in/talk/c3f16
    @SammyK #phpworld
    Framework pros
    The code is
    …so are the
    components you use
    when going bare
    battle tested

    View Slide

  148. joind.in/talk/c3f16
    @SammyK #phpworld
    Framework pros
    Great
    …so are the
    components you use
    when going bare
    documentation

    View Slide

  149. joind.in/talk/c3f16
    @SammyK #phpworld
    Framework pros
    Makes you more
    …going bare is a
    marketable skill
    marketable

    View Slide

  150. joind.in/talk/c3f16
    @SammyK #phpworld
    Framework pros
    They are often
    …in many contexts
    going bare is more
    pragmatic
    pragmatic

    View Slide

  151. joind.in/talk/c3f16
    @SammyK #phpworld
    myths?
    Did we bust the

    View Slide

  152. joind.in/talk/c3f16
    @SammyK #phpworld
    #1 You must write
    everything from scratch
    Nope,
    use components

    View Slide

  153. joind.in/talk/c3f16
    @SammyK #phpworld
    #2 You’re inherently anti-
    framework or anti-convention
    Nope, frameworks
    are great for most
    greenfield projects

    View Slide

  154. joind.in/talk/c3f16
    @SammyK #phpworld
    #3 You’ll spend most of your time
    on bootstrapping instead of on
    domain code
    It takes a little longer
    at first but you’ll get
    faster with practice

    View Slide

  155. joind.in/talk/c3f16
    @SammyK #phpworld
    #4 You must be an architecture
    astronaut
    Going bare is more
    pragmatic for legacy
    codebases

    View Slide

  156. joind.in/talk/c3f16
    @SammyK #phpworld
    #5 You like pain
    No, we just like to
    improve our skills

    View Slide

  157. joind.in/talk/c3f16
    @SammyK #phpworld

    View Slide

  158. joind.in/talk/c3f16
    @SammyK #phpworld
    Remember:
    It’s all about
    tradeoffs.

    View Slide

  159. joind.in/talk/c3f16
    @SammyK #phpworld
    Resources

    View Slide

  160. joind.in/talk/c3f16
    @SammyK #phpworld
    Create a PHP
    application without
    a framework
    Patrick Louys
    https://github.com/PatrickLouys/no-
    framework-tutorial

    View Slide

  161. joind.in/talk/c3f16
    @SammyK #phpworld
    Refactoring
    Martin Fowler

    View Slide

  162. joind.in/talk/c3f16
    @SammyK #phpworld
    <3 frameworks
    I
    I also <3 my skill for
    Going Bare

    View Slide

  163. Sammy Kaye Powers
    Thanks!
    /talk/c3f16
    @SammyK
    SammyK.me
    Host of @PHPRoundtable

    View Slide