Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
The Future of HTTP
In CakePHP
First, the Present
Request & Response
Added in 2.0.0
Symfony HttpKernel
& StackPHP
Symfony all
the Things
PSR-7
HTTP message interfaces
What’s the Problem?
HTTP in PHP is :(
$_POST
Only works for POST methods and url-encoded
data.
$_SERVER
No-consistency, or normalization
Request URI
Hard to find.
File Uploads
A pretty big mess.
Frameworks to
the Rescue!
Not Invented Here
Every framework has their own HTTP stack
No Shared Solutions
Sharing is caring
What do other
languages do?
Python
Web Server Gateway Interface
Ruby
Rack
Go-Lang
http module in stdlib
Common Traits
All these platforms have a single implementation.
PSR-7
Requests &
Responses
Requests
Immutable
All PSR7 objects are immutable*
// Read a header as text
$value = $request->getHeaderLine(‘Content-Type’);
// Read header as an array
$value = $request->g...
// Get an array of cookie values.
$cookies = $request->getCookieParams();
// Get a list of UploadedFile objects
$files = $...
// Get the URI
$uri = $request->getUri();
// Read data out of the URI.
$path = $uri->getPath();
$query = $uri->getQuery();...
// Get the application base dir
$base = $request->getAttribute(‘base’);
// Get the application webroot
$webroot = $request...
Responses
Also Immutable
// Bodies are also Streams.
$body = new Stream(‘php://memory');
$body->write(‘{“ok”:true}’);
// Build up a response.
$res-...
Middleware
CORS
Exceptions
Assets
Routes
App
CORS
Exceptions
Assets
Routes
App
Request
Response
CORS
Exceptions
Assets
Routes
App
Request
Response
1. Must accept a Request, Response and ‘next’
2. Must return a Response, or call ‘next’
3. No more Rules.
Middleware Rules
Middleware
Examples
$middleware = new CakeHttpMiddlewareStack();
// Catch any exceptions in the lower layers.
$middleware->push(new ErrorHandl...
// Using closures
$wow = function ($req, $res, $next) {
$res = $res->withHeader(‘X-Wow’, ‘Wow’);
return $next($req, $res);...
class WowMiddleware
{
public function __invoke($res, $req, $next)
{
$res = $res->withHeader(‘X-Wow’, ‘Wow’);
return $next(...
HttpClient
$client = new CakeHttpClient();
$res = $client->get(‘http://cakephp.org');
// Use PSR7 methods.
$status = $res->getStatusC...
Fully backwards
compatible
Client Responses still support the current
interface.
Sounds Nice, but
is upgrading going to suck?
Nope
It should be painless really.
Timeline
Now
3.3 - Fall 2016
3.4 - Early 2017
4.0 - ?
PSR-7 in 3.3.0
Opt-in stack that replaces DispatcherFilters
require dirname(__DIR__) . '/vendor/autoload.php';
use CakeHttpServer;
use AppApplication;
// Bind your application to the...
class Application extends BaseApplication
{
public function middleware($middleware)
{
// Catch any exceptions in the lower...
3.4.0
NetworkRequest will implement PSR7
Future 3.x.0
Runtime errors on deprecated methods.
4.0.0
Clean up. Remove deprecations.
In Review
1. PSR7 middleware is coming as an opt-in feature for 3.3.0
2. New applications will default to the new HTTP sta...
Thank You
Github - markstory
Twitter - mark_story
https://joind.in/talk/3b577
Future of HTTP in CakePHP
Upcoming SlideShare
Loading in …5
×

Future of HTTP in CakePHP

3,188 views

Published on

Slides from a talk I gave at CakeFest 2016, about the upcoming changes to CakePHP to support PSR7

Published in: Software
  • Be the first to comment

Future of HTTP in CakePHP

  1. 1. The Future of HTTP In CakePHP
  2. 2. First, the Present
  3. 3. Request & Response Added in 2.0.0
  4. 4. Symfony HttpKernel & StackPHP
  5. 5. Symfony all the Things
  6. 6. PSR-7 HTTP message interfaces
  7. 7. What’s the Problem?
  8. 8. HTTP in PHP is :(
  9. 9. $_POST Only works for POST methods and url-encoded data.
  10. 10. $_SERVER No-consistency, or normalization
  11. 11. Request URI Hard to find.
  12. 12. File Uploads A pretty big mess.
  13. 13. Frameworks to the Rescue!
  14. 14. Not Invented Here Every framework has their own HTTP stack
  15. 15. No Shared Solutions Sharing is caring
  16. 16. What do other languages do?
  17. 17. Python Web Server Gateway Interface
  18. 18. Ruby Rack
  19. 19. Go-Lang http module in stdlib
  20. 20. Common Traits All these platforms have a single implementation.
  21. 21. PSR-7
  22. 22. Requests & Responses
  23. 23. Requests
  24. 24. Immutable All PSR7 objects are immutable*
  25. 25. // 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
  26. 26. // 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
  27. 27. // Get the URI $uri = $request->getUri(); // Read data out of the URI. $path = $uri->getPath(); $query = $uri->getQuery(); $host = $uri->getHost(); URL data
  28. 28. // 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
  29. 29. Responses
  30. 30. Also Immutable
  31. 31. // 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
  32. 32. Middleware
  33. 33. CORS Exceptions Assets Routes App
  34. 34. CORS Exceptions Assets Routes App Request Response
  35. 35. CORS Exceptions Assets Routes App Request Response
  36. 36. 1. Must accept a Request, Response and ‘next’ 2. Must return a Response, or call ‘next’ 3. No more Rules. Middleware Rules
  37. 37. Middleware Examples
  38. 38. $middleware = new CakeHttpMiddlewareStack(); // 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
  39. 39. // 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
  40. 40. 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
  41. 41. HttpClient
  42. 42. $client = new CakeHttpClient(); $res = $client->get(‘http://cakephp.org'); // Use PSR7 methods. $status = $res->getStatusCode(); $contentType = $res->getHeaderLine(‘Content-Type’); $body = $res->getBody()->getContents(); Client Example
  43. 43. Fully backwards compatible Client Responses still support the current interface.
  44. 44. Sounds Nice, but is upgrading going to suck?
  45. 45. Nope It should be painless really.
  46. 46. Timeline Now 3.3 - Fall 2016 3.4 - Early 2017 4.0 - ?
  47. 47. PSR-7 in 3.3.0 Opt-in stack that replaces DispatcherFilters
  48. 48. require dirname(__DIR__) . '/vendor/autoload.php'; use CakeHttpServer; use AppApplication; // 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
  49. 49. 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
  50. 50. 3.4.0 NetworkRequest will implement PSR7
  51. 51. Future 3.x.0 Runtime errors on deprecated methods.
  52. 52. 4.0.0 Clean up. Remove deprecations.
  53. 53. In Review 1. 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.
  54. 54. Thank You Github - markstory Twitter - mark_story https://joind.in/talk/3b577

×