2. PSR – O QUE SIGNIFICA?
PHP
Standards
Recommendations
Número Título
1 Basic Coding Standard
2 Coding Style Guide
3 Logger Interface
4 Autoloading Standard
6 Caching Interface
7 HTTP Message Interface
3. PSR7 – MESSAGES
• Request
POST /path HTTP/1.1
Host: example.com
foo=bar&baz=bat
• PsrHttpMessageRequestInterface
• Response
HTTP/1.1 200 OK
Content-Type: text/plain
This is the response body
• PsrHttpMessageResponseInterface
5. PHP MIDDLEWARES
• Um middleware recebe uma Request, uma Response e faz algo
com elas.
• Todo middleware DEVE retornar uma
PsrHttpMessageResponseInterface
• Todo middleware pode chamar o próximo da fila
7. FLUXO COM MIDDLEWARE
$app = new MiddlewareRunner();
$app->add('/contact', new ContactFormMiddleware());
$app->add('/forum', new ForumMiddleware());
$app->add('/blog', new BlogMiddleware());
$app->add('/store', new EcommerceMiddleware());
$app->run($request, $response);
$app = new MiddlewareRunner();
$app->add('/zf2', new Zf2Middleware());
$app->add('/symfony', new SymfonyMiddleware());
$app->add('/cake', new CakeMiddleware());
$app->add('/laravel', new LaravelMiddleware());
$app->run($request, $response);
8. APLICAÇÃO COM MIDDLEWARE
use ZendStratigilityMiddlewarePipe;
use ZendDiactorosServer;
require __DIR__ . '/../vendor/autoload.php';
$app = new MiddlewarePipe();
$server = Server::createServer($app, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
$app->pipe('/', function ($req, $res, $next) {
if (! in_array($req->getUri()->getPath(), ['/', ''], true)) {
return $next($req, $res);
}
return $res->end('Hello world!');
});
$app->pipe('/foo', function ($req, $res, $next) {
return $res->end('FOO!');
});
$server->listen();