Slide 1

Slide 1 text

Composing PHP Applications with Middleware Josh Butts PHP World 2016

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

PSR-7 Lets Start With 3

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

DID WE END UP HERE? How in the hell 8

Slide 9

Slide 9 text

PSR-7 Enter 9

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

PSR-7 is a collection of Interfaces 11

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

MIDDLEWARE I thought this talk was about 13

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

What does a middleware look like? 20

Slide 21

Slide 21 text

It might also look like this 21

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

LETS LOOK AT CODE All that’s fine 23

Slide 24

Slide 24 text

QUESTIONS? 24

Slide 25

Slide 25 text

GITHUB.COM/JIMBOJSB/ MIDDLEWARE-TALK 25

Slide 26

Slide 26 text

JOIND.IN/TALK/AF8F4 I’d love your feedback 26