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

Composing PHP Applications with Middleware (Zendcon 2016)

Josh Butts
October 20, 2016

Composing PHP Applications with Middleware (Zendcon 2016)

With the advent of the PSR-7 standard, middleware has become a household name in the PHP ecosystem. This talk will cover middleware architecture concepts including a comparison of how middleware is being used in PHP versus other languages. We'll look at how to leverage middleware concepts to build applications from scratch as well as combine off-the-shelf components using middleware as the glue. Of course, we'll also look at some downright dirty tricks that middleware lets you get away with as well.

Josh Butts

October 20, 2016
Tweet

More Decks by Josh Butts

Other Decks in Technology

Transcript

  1. Composing PHP Applications with Middleware
    Josh Butts
    Zendcon 2016

    View Slide

  2. About Me
    • VP of Engineering, offers.com
    • Austin PHP Organizer
    • Play competitive Skee Ball
    • github.com/jimbojsb
    • @jimbojsb
    2

    View Slide

  3. PSR-7
    Lets Start With
    3

    View Slide

  4. What is PSR-7
    • FIG standard for HTTP messages
    • A collection of interfaces
    • By itself, has nothing specific to do with
    middleware
    4

    View Slide

  5. Request - Historically
    • $_SERVER, $_GET, $_POST<, etc
    • apache_request_headers()
    • php://input
    5

    View Slide

  6. Response - Historically
    • header()
    • http_response_code()
    • header_list()
    • echo
    6

    View Slide

  7. URI - Historically
    • parse_url()
    • parse_str()
    • http_build_query()
    7

    View Slide

  8. DID WE END UP HERE?
    How in the hell
    8

    View Slide

  9. PSR-7
    Enter
    9

    View Slide

  10. But first - More History
    • 2014 - everyone* has gone out and built
    and object model of HTTP
    • Hello World for writing a framework
    • None of them are compatible
    • Symfony\HttpFoundation widely used
    10

    View Slide

  11. PSR-7 is a collection of Interfaces
    11

    View Slide

  12. Concrete Implementations of PSR-7
    • Zend\Diactoros
    • GuzzleHTTP\Psr7
    • Slim\Http
    12

    View Slide

  13. MIDDLEWARE
    I thought this talk was about
    13

    View Slide

  14. What is middleware?
    • Turns a Request into a Response
    • Stackable
    • Reusable
    • Dispatachable
    14

    View Slide

  15. PHP Copied This Trend
    • Node.js Connect
    • Ruby Rack
    • Python WSGI
    • Even StackPHP
    15

    View Slide

  16. Express.js is getting it right
    • All middleware all the way down
    • Even error handlers are middleware
    • Even the app instance itself is middleware
    16

    View Slide

  17. Components of a PHP Middleware App
    • PSR-7 implementation
    • Collection of middleware
    • Middleware dispatcher
    • Any other libraries that will actually do
    business logic for you
    17

    View Slide

  18. Pick a dispatcher
    • Zend-Stratagility
    • Relay
    • Radar
    • Slim v3
    • Zend Expressive
    18

    View Slide

  19. Pick a PSR-7 Implementation
    • Do not write one
    • Unless you’re using Slim already just use
    Zend/Diactoros
    19

    View Slide

  20. What does a middleware look like?
    20
    $middleware = function(
    \Psr\Http\Message\RequestInterface $request,
    \Psr\Http\Message\ResponseInterface $response,
    callable $next) {
    // do the actual stuff
    };

    View Slide

  21. It might also look like this
    21
    $middleware = function(
    \Psr\Http\Message\RequestInterface $request,
    callable $next) {
    // do some stuff
    };

    View Slide

  22. PSR-15
    • There is still some debate as to which is
    better
    • It’s like the standard will not pass along the
    response
    • Lots of existing middleware out there that
    would not meet this spec
    22

    View Slide

  23. LETS LOOK AT CODE
    All that’s fine
    23

    View Slide

  24. QUESTIONS?
    24

    View Slide

  25. JOIND.IN/TALK/70548
    I’d love your feedback
    25

    View Slide