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. // 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. // Get the URI
$uri = $request->getUri();
// Read data out of the URI.
$path = $uri->getPath();
$query = $uri->getQuery();
$host = $uri->getHost();
URL data
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
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
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. // 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. 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
49. 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
50. 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
54. 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.
55. Thank You
Github - markstory
Twitter - mark_story
https://joind.in/talk/3b577