Future of HTTP in CakePHP

55
The Future of HTTP In CakePHP

Transcript of Future of HTTP in CakePHP

Page 1: Future of HTTP in CakePHP

The Future of HTTPIn CakePHP

Page 2: Future of HTTP in CakePHP

First, the Present

Page 3: Future of HTTP in CakePHP

Request & ResponseAdded in 2.0.0

Page 4: Future of HTTP in CakePHP

Symfony HttpKernel& StackPHP

Page 5: Future of HTTP in CakePHP

Symfony all the Things

Page 6: Future of HTTP in CakePHP

PSR-7HTTP message interfaces

Page 7: Future of HTTP in CakePHP

What’s the Problem?

Page 8: Future of HTTP in CakePHP

HTTP in PHP is :(

Page 9: Future of HTTP in CakePHP

$_POSTOnly works for POST methods and url-encoded

data.

Page 10: Future of HTTP in CakePHP

$_SERVERNo-consistency, or normalization

Page 11: Future of HTTP in CakePHP

Request URIHard to find.

Page 12: Future of HTTP in CakePHP

File UploadsA pretty big mess.

Page 13: Future of HTTP in CakePHP

Frameworks to the Rescue!

Page 14: Future of HTTP in CakePHP

Not Invented HereEvery framework has their own HTTP stack

Page 15: Future of HTTP in CakePHP

No Shared SolutionsSharing is caring

Page 16: Future of HTTP in CakePHP

What do other languages do?

Page 17: Future of HTTP in CakePHP

PythonWeb Server Gateway Interface

Page 18: Future of HTTP in CakePHP

RubyRack

Page 19: Future of HTTP in CakePHP

Go-Langhttp module in stdlib

Page 20: Future of HTTP in CakePHP

Common TraitsAll these platforms have a single implementation.

Page 21: Future of HTTP in CakePHP

PSR-7

Page 22: Future of HTTP in CakePHP

Requests & Responses

Page 23: Future of HTTP in CakePHP

Requests

Page 24: Future of HTTP in CakePHP

ImmutableAll PSR7 objects are immutable*

Page 25: Future of HTTP in CakePHP

// Read a header as text $value = $request->getHeaderLine(‘Content-Type’);

// Read header as an array $value = $request->getHeader(‘Content-Type’);

// Read all the headers $headers = $request->getHeaders();

Reading Headers

Page 26: Future of HTTP in CakePHP

// Get an array of cookie values. $cookies = $request->getCookieParams();

// Get a list of UploadedFile objects $files = $request->getUploadedFiles();

// Read the file data. $files[0]->getStream(); $files[0]->getSize(); $files[0]->getClientFileName();

// Move the file. $files[0]->moveTo($targetPath);

Cookies & Files

Page 27: Future of HTTP in CakePHP

// Get the URI $uri = $request->getUri();

// Read data out of the URI. $path = $uri->getPath(); $query = $uri->getQuery(); $host = $uri->getHost();

URL data

Page 28: Future of HTTP in CakePHP

// Get the application base dir $base = $request->getAttribute(‘base’);

// Get the application webroot $webroot = $request->getAttribute(‘webroot’);

// Get the routing parameters $params = $request->getAttribute(‘params’);

Request Attributes

Page 29: Future of HTTP in CakePHP

Responses

Page 30: Future of HTTP in CakePHP

Also Immutable

Page 31: Future of HTTP in CakePHP

// Bodies are also Streams. $body = new Stream(‘php://memory'); $body->write(‘{“ok”:true}’);

// Build up a response. $res->withHeader(‘Content-Type’, ‘application/json’) ->withStatus(204) ->withBody($body);

Building Responses

Page 32: Future of HTTP in CakePHP

Middleware

Page 33: Future of HTTP in CakePHP

CORS

Exceptions

Assets

Routes

App

Page 34: Future of HTTP in CakePHP

CORS

Exceptions

Assets

Routes

App

Request

Response

Page 35: Future of HTTP in CakePHP

CORS

Exceptions

Assets

Routes

AppRequest

Response

Page 36: Future of HTTP in CakePHP

1. Must accept a Request, Response and ‘next’

2. Must return a Response, or call ‘next’

3. No more Rules.

Middleware Rules

Page 37: Future of HTTP in CakePHP

Middleware Examples

Page 38: Future of HTTP in CakePHP

$middleware = new \Cake\Http\MiddlewareStack();

// Catch any exceptions in the lower layers. $middleware->push(new ErrorHandlerMiddleware());

// Handle plugin/theme assets $middleware->push(new AssetMiddleware());

// Apply routing $middleware->push(new RoutingMiddleware());

// Apply CORS at the first middleware $middleware->prepend(new CorsMiddleware());

Setup Middleware

Page 39: Future of HTTP in CakePHP

// Using closures $wow = function ($req, $res, $next) { $res = $res->withHeader(‘X-Wow’, ‘Wow’); return $next($req, $res); };

// Add to a middleware stack $middleware->push($wow);

Middleware

Page 40: Future of HTTP in CakePHP

class WowMiddleware { public function __invoke($res, $req, $next) { $res = $res->withHeader(‘X-Wow’, ‘Wow’); return $next($req, $res); } }

// Add to a middleware stack $middleware->push(new WowMiddleware());

Middleware

Page 41: Future of HTTP in CakePHP

Http\Client

Page 42: Future of HTTP in CakePHP

$client = new Cake\Http\Client();

$res = $client->get(‘http://cakephp.org');

// Use PSR7 methods. $status = $res->getStatusCode(); $contentType = $res->getHeaderLine(‘Content-Type’); $body = $res->getBody()->getContents();

Client Example

Page 43: Future of HTTP in CakePHP

Fully backwards compatible

Client Responses still support the current interface.

Page 44: Future of HTTP in CakePHP
Page 45: Future of HTTP in CakePHP

Sounds Nice, butis upgrading going to suck?

Page 46: Future of HTTP in CakePHP

NopeIt should be painless really.

Page 47: Future of HTTP in CakePHP

Timeline

Now

3.3 - Fall 2016

3.4 - Early 2017

4.0 - ?

Page 48: Future of HTTP in CakePHP

PSR-7 in 3.3.0Opt-in stack that replaces DispatcherFilters

Page 49: Future of HTTP in CakePHP

require dirname(__DIR__) . '/vendor/autoload.php';

use Cake\Http\Server; use App\Application;

// Bind your application to the server. $app = new Application(dirname(__DIR__) . ‘/config'); $server = new Server($app);

// Run the request/response and emit the response $server->emit($server->run());

index.php of the future

Page 50: Future of HTTP in CakePHP

class Application extends BaseApplication { public function middleware($middleware) { // Catch any exceptions in the lower layers. $middleware->push(new ErrorHandlerMiddleware());

// Handle plugin/theme assets $middleware->push(new AssetMiddleware());

// Apply routing $middleware->push(new RoutingMiddleware()); return $middleware; } }

Application Class

Page 51: Future of HTTP in CakePHP

3.4.0 Network\Request will implement PSR7

Page 52: Future of HTTP in CakePHP

Future 3.x.0Runtime errors on deprecated methods.

Page 53: Future of HTTP in CakePHP

4.0.0Clean up. Remove deprecations.

Page 54: Future of HTTP in CakePHP

In Review1. PSR7 middleware is coming as an opt-in feature for 3.3.0

2. New applications will default to the new HTTP stack in 3.3.0

3. Controllers will have access to PSR7 methods in 3.4.0

4. Runtime deprecations will be introduced in a future 3.x

5. The code you write today will continue to work until 4.0.0.

Page 55: Future of HTTP in CakePHP

Thank YouGithub - markstory

Twitter - mark_story https://joind.in/talk/3b577