Pro Yearly is on sale from $80 to $50! »

Going Bare - Writing the web without a framework - ZendCon 2017

Going Bare - Writing the web without a framework - ZendCon 2017

Talk given at ZendCon 2017

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 session. Being comfortable developing without a framework is a crucial skill to have for refactoring legacy applications. In this session you'll see 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.

8c090cc1ccd623a146ddd9159b1bf7e2?s=128

Sammy Kaye Powers

October 25, 2017
Tweet

Transcript

  1. Going Bare Writing the web without a framework Sammy Kaye

    Powers 2017-10-25
  2. joind.in/talk/d9895 @SammyK #ZendCon2017 Slides Get the joind.in/talk/d9895

  3. joind.in/talk/d9895 @SammyK #ZendCon2017 frameworks The point is not <3 frameworks!

    I
  4. joind.in/talk/d9895 @SammyK #ZendCon2017 opinion It’s OK to have an

  5. joind.in/talk/d9895 @SammyK #ZendCon2017 experiment It’s OK to

  6. joind.in/talk/d9895 @SammyK #ZendCon2017 Bootstrapping / ˈbo ͞ otˌstraˌpiNG / Define:

    (…at least for this talk) Code that acts as an entry point into an app.
  7. joind.in/talk/d9895 @SammyK #ZendCon2017 Boilerplate / ˈboilərˌplāt / Define: The “glue”

    code that ties components together. (…at least for this talk)
  8. joind.in/talk/d9895 @SammyK #ZendCon2017 Frameworks are a tool

  9. joind.in/talk/d9895 @SammyK #ZendCon2017 Going bare is a skill (that you

    don’t get with a framework)
  10. joind.in/talk/d9895 @SammyK #ZendCon2017 The of going bare skill • Writing

    bootstrap & boilerplate • Choosing exact components • Use different paradigms (Get to know the component ecosystem) (Like singleton, et al) (Gives you more control over how components interact)
  11. joind.in/talk/d9895 @SammyK #ZendCon2017 Myths about going frameworkless

  12. joind.in/talk/d9895 @SammyK #ZendCon2017 Myth #1 You must write everything from

    scratch
  13. joind.in/talk/d9895 @SammyK #ZendCon2017 Myth #2 You’re inherently anti-framework or anti-convention

  14. joind.in/talk/d9895 @SammyK #ZendCon2017 Myth #3 You’ll spend most of your

    time on boilerplate instead of on domain code
  15. joind.in/talk/d9895 @SammyK #ZendCon2017 Myth #4 You must be an architecture

    astronaut
  16. joind.in/talk/d9895 @SammyK #ZendCon2017 Myth #5 You like pain

  17. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros Framework Cons

  18. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros

  19. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros

  20. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros crazy fast You get started

  21. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros First-time install time minutes! 5

  22. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros

  23. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros boilerplate all the things You don’t

    have to Con but it can be a
  24. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros bootstrap & boilerplate code You don’t

    have to maintain
  25. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros battle-tested The code is

  26. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros conventions Ready-to-follow Con but it can

    be a
  27. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros documentation Most frameworks have great

  28. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros marketable It makes you more

  29. joind.in/talk/d9895 @SammyK #ZendCon2017 Pros pragmatic They are often

  30. joind.in/talk/d9895 @SammyK #ZendCon2017 cons? What about the

  31. joind.in/talk/d9895 @SammyK #ZendCon2017 Cons legacy codebases Useless for

  32. joind.in/talk/d9895 @SammyK #ZendCon2017 Cons cruft They fill your project with

  33. joind.in/talk/d9895 @SammyK #ZendCon2017 Cons Cruft • Deployment takes longer •

    Upgrading pulls in all the new features
  34. joind.in/talk/d9895 @SammyK #ZendCon2017 Cons tightly couple Easier to due to

    convenience
  35. joind.in/talk/d9895 @SammyK #ZendCon2017 The The Cons Pros seem great are

    weak…
  36. joind.in/talk/d9895 @SammyK #ZendCon2017 Why would you go bare? (To answer

    we must ask…)
  37. joind.in/talk/d9895 @SammyK #ZendCon2017 When should you go bare?

  38. joind.in/talk/d9895 @SammyK #ZendCon2017

  39. joind.in/talk/d9895 @SammyK #ZendCon2017 Greenfield Legacy Depends

  40. joind.in/talk/d9895 @SammyK #ZendCon2017 5% 95% 90% 10% Go Bare Framwork

    Greenfield Legacy When I go bare
  41. joind.in/talk/d9895 @SammyK #ZendCon2017 How do you go bare?

  42. joind.in/talk/d9895 @SammyK #ZendCon2017 from scratch You could just write

  43. joind.in/talk/d9895 @SammyK #ZendCon2017

  44. joind.in/talk/d9895 @SammyK #ZendCon2017 That means writing all the things Router

    DBAL/ORM Dep Injection Logger
  45. joind.in/talk/d9895 @SammyK #ZendCon2017 reinventing the wheel But that’s like

  46. joind.in/talk/d9895 @SammyK #ZendCon2017 Better Way™ There’s a

  47. joind.in/talk/d9895 @SammyK #ZendCon2017 po- nents Com-

  48. joind.in/talk/d9895 @SammyK #ZendCon2017 components has a healthy ecosystem of

  49. joind.in/talk/d9895 @SammyK #ZendCon2017 using them You’re probably already

  50. joind.in/talk/d9895 @SammyK #ZendCon2017 built on them Most frameworks are zend-router

    zend-db zend-di zend-log
  51. joind.in/talk/d9895 @SammyK #ZendCon2017 choose ur candy Going bare lets you

    Look at all those smarties! Yum!
  52. joind.in/talk/d9895 @SammyK #ZendCon2017 I like smarties

  53. joind.in/talk/d9895 @SammyK #ZendCon2017 Router DBAL/ORM Dep Injection Logger

  54. joind.in/talk/d9895 @SammyK #ZendCon2017 zend-router fast-route php-router phroute Routers Tons of

    variety
  55. joind.in/talk/d9895 @SammyK #ZendCon2017 Easily installed with

  56. joind.in/talk/d9895 @SammyK #ZendCon2017 bag o’ candy A framework is like

    a Comes with licorice? Yuck!
  57. joind.in/talk/d9895 @SammyK #ZendCon2017 I don’t like licorice

  58. joind.in/talk/d9895 @SammyK #ZendCon2017 components When you go bare don’t just

    magically start working together
  59. joind.in/talk/d9895 @SammyK #ZendCon2017 Key: Get comfy with bootstrapping & boilerplate

    code It’s really not that bad
  60. joind.in/talk/d9895 @SammyK #ZendCon2017 examples Real-world

  61. joind.in/talk/d9895 @SammyK #ZendCon2017 conventions When going bare, there aren’t any

    …so this is a way, not the way
  62. joind.in/talk/d9895 @SammyK #ZendCon2017 Greenfield Let’s start with

  63. joind.in/talk/d9895 @SammyK #ZendCon2017 Case study Raw CSV Calculate
 & save

    to DB Show report Client’s server Our server
  64. joind.in/talk/d9895 @SammyK #ZendCon2017 Case study Download CSV & calculate CLI

    View the report HTTP Endpoint
  65. joind.in/talk/d9895 @SammyK #ZendCon2017 coding domain in 5 mins Install framework

    &
  66. joind.in/talk/d9895 @SammyK #ZendCon2017 61components installed before we code https://framework.zend.com/downloads

  67. joind.in/talk/d9895 @SammyK #ZendCon2017 go micro We could… Slim Silex (et

    al)
  68. joind.in/talk/d9895 @SammyK #ZendCon2017 go bare! :) But let’s

  69. joind.in/talk/d9895 @SammyK #ZendCon2017 Start in the CLI (Just like when

    you use a framework!)
  70. joind.in/talk/d9895 @SammyK #ZendCon2017 1. Make an empty composer.json or… $

    composer init
  71. joind.in/talk/d9895 @SammyK #ZendCon2017 2. Make a folder for domain code

    $ mkdir src
  72. joind.in/talk/d9895 @SammyK #ZendCon2017 3. Add autoloading $ composer \ dumpautoload

    And then run…
  73. joind.in/talk/d9895 @SammyK #ZendCon2017 Now just install your components & tie

    them together
  74. joind.in/talk/d9895 @SammyK #ZendCon2017 Key: Get comfy with bootstrapping & boilerplate

    code It’s really not that bad
  75. joind.in/talk/d9895 @SammyK #ZendCon2017 Case study Download CSV & calculate CLI

    View the report HTTP Endpoint
  76. joind.in/talk/d9895 @SammyK #ZendCon2017 Install a console component $ composer require

    symfony/console (or maybe you like zendframework/zend-console)
  77. joind.in/talk/d9895 @SammyK #ZendCon2017 src/Console/Runner.php

  78. joind.in/talk/d9895 @SammyK #ZendCon2017 We get to make our first bootstrap

    code!
  79. joind.in/talk/d9895 @SammyK #ZendCon2017 run (sans .php extension) $ chmod +x

    run
  80. joind.in/talk/d9895 @SammyK #ZendCon2017 $ ./run

  81. joind.in/talk/d9895 @SammyK #ZendCon2017 B.A.U. Now it’s just src/Console/Runner.php

  82. joind.in/talk/d9895 @SammyK #ZendCon2017 src/Console/CalculateReportsCommand.php

  83. joind.in/talk/d9895 @SammyK #ZendCon2017 $ ./run

  84. joind.in/talk/d9895 @SammyK #ZendCon2017

  85. joind.in/talk/d9895 @SammyK #ZendCon2017 Congrats!

  86. joind.in/talk/d9895 @SammyK #ZendCon2017 Install the DI container $ composer require

    pimple/pimple (Choose your poison)
  87. joind.in/talk/d9895 @SammyK #ZendCon2017 Need to make singleton ready src/Application.php

  88. joind.in/talk/d9895 @SammyK #ZendCon2017 Singleton Isn’t that an anti-pattern?

  89. joind.in/talk/d9895 @SammyK #ZendCon2017 https://en.wikipedia.org/wiki/Singleton_pattern

  90. joind.in/talk/d9895 @SammyK #ZendCon2017

  91. joind.in/talk/d9895 @SammyK #ZendCon2017 Singleton •Ensures one & only one instance

    •Global access (static function) •Initialization on first use
  92. joind.in/talk/d9895 @SammyK #ZendCon2017 Fatal error: Call to private App\Application::__construct() src/Application.php

  93. joind.in/talk/d9895 @SammyK #ZendCon2017 src/Application.php Global access + instantiate on first

    use
  94. joind.in/talk/d9895 @SammyK #ZendCon2017 Get an instance of Application

  95. joind.in/talk/d9895 @SammyK #ZendCon2017 Prevent cloning src/Application.php

  96. joind.in/talk/d9895 @SammyK #ZendCon2017 src/Application.php Prevent unserialization

  97. joind.in/talk/d9895 @SammyK #ZendCon2017 •Ensures one & only one instance •Global

    access (static function) •Initialization on first use
  98. joind.in/talk/d9895 @SammyK #ZendCon2017 Case study Download CSV & calculate CLI

    View the report HTTP Endpoint
  99. joind.in/talk/d9895 @SammyK #ZendCon2017 We get to make our final bootstrap

    code!
  100. joind.in/talk/d9895 @SammyK #ZendCon2017 public/index.php

  101. joind.in/talk/d9895 @SammyK #ZendCon2017 Eh?

  102. joind.in/talk/d9895 @SammyK #ZendCon2017 src/Application.php However you register service providers into

    the container (different DI containers have their own API)
  103. joind.in/talk/d9895 @SammyK #ZendCon2017 Why not in constructor?

  104. joind.in/talk/d9895 @SammyK #ZendCon2017 Install a router $ composer require nikic/fast-route

    (Choose your poison)
  105. joind.in/talk/d9895 @SammyK #ZendCon2017 service provider Make a routing Pimple syntax

    - mileage my vary
  106. joind.in/talk/d9895 @SammyK #ZendCon2017 Register in container src/Application.php

  107. joind.in/talk/d9895 @SammyK #ZendCon2017 src/Router.php

  108. joind.in/talk/d9895 @SammyK #ZendCon2017 Ugly A.F. src/routes.php

  109. joind.in/talk/d9895 @SammyK #ZendCon2017 Different project, same component src/routes.php

  110. joind.in/talk/d9895 @SammyK #ZendCon2017 public/index.php run We got bootstrap Runs CLI

    Runs HTTP
  111. joind.in/talk/d9895 @SammyK #ZendCon2017 We got boilerplate src/Router.php src/Console/Runner.php symfony/console nikic/fast-route

  112. joind.in/talk/d9895 @SammyK #ZendCon2017 Still not convinced?

  113. joind.in/talk/d9895 @SammyK #ZendCon2017 What about Legacy

  114. joind.in/talk/d9895 @SammyK #ZendCon2017 Why would you go bare? It’s a

    skill you can use for legacy apps
  115. joind.in/talk/d9895 @SammyK #ZendCon2017 I’ll just rewrite this in x framework…

  116. joind.in/talk/d9895 @SammyK #ZendCon2017 Totes samezies except routing

  117. joind.in/talk/d9895 @SammyK #ZendCon2017 Route exists? Not found Router Yes No

    Code 404
  118. joind.in/talk/d9895 @SammyK #ZendCon2017 Route exists? Legacy router New router Yes

    No New code
  119. joind.in/talk/d9895 @SammyK #ZendCon2017 app/webroot/index.php Old CakePHP bootstrap file

  120. joind.in/talk/d9895 @SammyK #ZendCon2017 src/bootstrap.php Same as public/index.php for HTTP

  121. joind.in/talk/d9895 @SammyK #ZendCon2017 src/Router.php

  122. joind.in/talk/d9895 @SammyK #ZendCon2017 tests? What about

  123. joind.in/talk/d9895 @SammyK #ZendCon2017 Install PHPUnit $ composer require phpunit/phpunit --dev

  124. joind.in/talk/d9895 @SammyK #ZendCon2017 phpunit.xml

  125. joind.in/talk/d9895 @SammyK #ZendCon2017 tests/bootstrap.php

  126. joind.in/talk/d9895 @SammyK #ZendCon2017 src/Application.php

  127. joind.in/talk/d9895 @SammyK #ZendCon2017 Run the test suite $ ./vendor/bin/phpunit

  128. joind.in/talk/d9895 @SammyK #ZendCon2017 How to choose your components

  129. joind.in/talk/d9895 @SammyK #ZendCon2017 Use more than one framework #1

  130. joind.in/talk/d9895 @SammyK #ZendCon2017 Search packagist.org #2 (Make sure it has

    good docs & is actively maintained)
  131. joind.in/talk/d9895 @SammyK #ZendCon2017 experiments Try some

  132. joind.in/talk/d9895 @SammyK #ZendCon2017 100% middleware My next experiment: (I could

    totes use Zend Expressive or just go bare)
  133. joind.in/talk/d9895 @SammyK #ZendCon2017 pros? framework ‘member the

  134. joind.in/talk/d9895 @SammyK #ZendCon2017 Framework pros Crazy fast to get started

    …but you’ll get faster at going bare
  135. joind.in/talk/d9895 @SammyK #ZendCon2017 Framework pros to maintain …most the time

    it’s really not that bad No bootstrap & boilerplate code
  136. joind.in/talk/d9895 @SammyK #ZendCon2017 Framework pros The code is …so are

    the components you use when going bare battle tested
  137. joind.in/talk/d9895 @SammyK #ZendCon2017 Framework pros Great …so are the components

    you use when going bare documentation
  138. joind.in/talk/d9895 @SammyK #ZendCon2017 Framework pros Makes you more …going bare

    is a marketable skill marketable
  139. joind.in/talk/d9895 @SammyK #ZendCon2017 Framework pros They are often …in many

    contexts going bare is more pragmatic pragmatic
  140. joind.in/talk/d9895 @SammyK #ZendCon2017 myths? Did we bust the

  141. joind.in/talk/d9895 @SammyK #ZendCon2017 #1 You must write everything from scratch

    Nope, use components
  142. joind.in/talk/d9895 @SammyK #ZendCon2017 #2 You’re inherently anti- framework or anti-convention

    Nope, frameworks are great for most greenfield projects
  143. joind.in/talk/d9895 @SammyK #ZendCon2017 #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
  144. joind.in/talk/d9895 @SammyK #ZendCon2017 #4 You must be an architecture astronaut

    Going bare is more pragmatic for legacy codebases
  145. joind.in/talk/d9895 @SammyK #ZendCon2017 #5 You like pain No, we just

    like to improve our skills
  146. joind.in/talk/d9895 @SammyK #ZendCon2017

  147. joind.in/talk/d9895 @SammyK #ZendCon2017 Remember: It’s all about tradeoffs.

  148. joind.in/talk/d9895 @SammyK #ZendCon2017 Resources

  149. joind.in/talk/d9895 @SammyK #ZendCon2017 Create a PHP application without a framework

    Patrick Louys https://github.com/PatrickLouys/no- framework-tutorial
  150. joind.in/talk/d9895 @SammyK #ZendCon2017 Refactoring Martin Fowler

  151. joind.in/talk/d9895 @SammyK #ZendCon2017 <3 frameworks I I also <3 my

    skill for Going Bare
  152. Sammy Kaye Powers Thanks! /talk/d9895 @SammyK SammyK.me Host of @PHPRoundtable

    Co-admin of @ChiPHPUG I ❤ West Coast Swing