SlideShare a Scribd company logo
ZOOX
DEVTALK #2
PSR-7 E MIDDLEWARES
JULHO / 2016
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
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
PSR7 - HEADERS
$message = $message->withHeader('foo', 'bar');
echo $message->getHeaderLine('foo');
// bar
echo $message->getHeaderLine('FOO');
// bar
$message = $message->withHeader('fOO', 'baz');
echo $message->getHeaderLine('foo');
// baz
$message = $message
->withHeader('foo', 'bar')
->withAddedHeader('foo', 'baz');
$header = $message->getHeaderLine('foo');
// 'bar, baz'
$header = $message->getHeader('foo');
// ['bar', 'baz']
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
MIDDLEWARE - EXEMPLO
<?php
/**
* Exemplo de um middleware
*
* @param PsrHttpMessageServerRequestInterface $request PSR7 request
* @param PsrHttpMessageResponseInterface $response PSR7 response
* @param callable $next Próximo
*
* @return PsrHttpMessageResponseInterface
*/
function ($request, $response, $next)
{
$response->getBody()->write('BEFORE');
$response = $next($request, $response);
$response->getBody()->write('AFTER');
return $response;
};
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);
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();
ZEND EXPRESSIVE
• Microframework voltada para PSR-7 middlewares
• Recursos:
• Routing
• Container (container-interopt)
• Templating
• Error Handling
• Pipelines
EXPRESSIVE - EXEMPLO
<?php
use ZendExpressiveAppFactory;
chdir(dirname(__DIR__));
require 'vendor/autoload.php';
$app = AppFactory::create();
$app->get('/', function ($request, $response, $next) {
$response->getBody()->write('Hello, world!');
return $response;
});
$app->pipeRoutingMiddleware();
$app->pipeDispatchMiddleware();
$app->run();

More Related Content

PDF
Jwt, wtf? - Phil Nash - Codemotion Amsterdam 2017
ODP
Whatsnew in-perl
PDF
Leveraging APIs without Programming in FME Server Provisioning
PPTX
Tactics for Painless Public Technology Training (Communities of Practice)
PPTX
Pp curricular evidencia
PDF
Independence mag july2014
PPTX
Growth and Decay Word Problems
Jwt, wtf? - Phil Nash - Codemotion Amsterdam 2017
Whatsnew in-perl
Leveraging APIs without Programming in FME Server Provisioning
Tactics for Painless Public Technology Training (Communities of Practice)
Pp curricular evidencia
Independence mag july2014
Growth and Decay Word Problems

Viewers also liked (15)

PPTX
Web Designing Company in India
PPTX
EDA DE PAZ
PDF
International Journal of Computational Science and Information Technology (I...
PPT
2015 CIC: #EdTech Forum - Understanding and Influencing Publishing Standards
DOCX
Argumente per rrugen e drejte
PDF
La Commission Interaméricaine des Droits de l'Homme constate la vacance Prési...
PPTX
MyDailyChoice Opportunity Presentation
PPTX
Grecia antigua periodo pericles
PPTX
Solving linear trig equations
PDF
Lights, Camera, Advocacy: Digital Storytelling for Your Library
PDF
CdP Renault bilan 2010_2013
PDF
historia del dibujo. Reseña.
PPTX
10 TERMETET DHE 10 VULLKANET ME TE MEDHENJ NE BOTE
PPT
Arte griego
PDF
Sani plot 2015 ok
Web Designing Company in India
EDA DE PAZ
International Journal of Computational Science and Information Technology (I...
2015 CIC: #EdTech Forum - Understanding and Influencing Publishing Standards
Argumente per rrugen e drejte
La Commission Interaméricaine des Droits de l'Homme constate la vacance Prési...
MyDailyChoice Opportunity Presentation
Grecia antigua periodo pericles
Solving linear trig equations
Lights, Camera, Advocacy: Digital Storytelling for Your Library
CdP Renault bilan 2010_2013
historia del dibujo. Reseña.
10 TERMETET DHE 10 VULLKANET ME TE MEDHENJ NE BOTE
Arte griego
Sani plot 2015 ok
Ad

Similar to Dev talk2 (20)

PDF
PHP Programming and its Applications workshop
PPT
php_postgresql.pptyyguyg7g7g777g76776777
PPTX
Introduction to PHP Lecture 1
PPT
php_postgresql.ppt
PPT
Introduction to php and POSTGRESQL. ....
PPT
PHP with Postgres SQL connection string and connecting
PPT
php_postgresql.ppt
PDF
PSR-7 - HTTP message interfaces
PPT
Php performance
PPTX
PHP5.3 を使うのはやめよう
PDF
Submit PHP: Standards in PHP world. Михайло Морозов
KEY
GettingStartedWithPHP
PPT
Intro to PHP
PPT
Introducation to php for beginners
PPT
Introduction to PHP
PPT
Php with my sql
PPTX
PPTX
php (Hypertext Preprocessor)
PDF
Key features PHP 5.3 - 5.6
PPTX
Ch1(introduction to php)
PHP Programming and its Applications workshop
php_postgresql.pptyyguyg7g7g777g76776777
Introduction to PHP Lecture 1
php_postgresql.ppt
Introduction to php and POSTGRESQL. ....
PHP with Postgres SQL connection string and connecting
php_postgresql.ppt
PSR-7 - HTTP message interfaces
Php performance
PHP5.3 を使うのはやめよう
Submit PHP: Standards in PHP world. Михайло Морозов
GettingStartedWithPHP
Intro to PHP
Introducation to php for beginners
Introduction to PHP
Php with my sql
php (Hypertext Preprocessor)
Key features PHP 5.3 - 5.6
Ch1(introduction to php)
Ad

Recently uploaded (20)

PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Mushroom cultivation and it's methods.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Getting Started with Data Integration: FME Form 101
PDF
August Patch Tuesday
PDF
Empathic Computing: Creating Shared Understanding
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Assigned Numbers - 2025 - Bluetooth® Document
OMC Textile Division Presentation 2021.pptx
Encapsulation_ Review paper, used for researhc scholars
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Spectroscopy.pptx food analysis technology
Mushroom cultivation and it's methods.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Getting Started with Data Integration: FME Form 101
August Patch Tuesday
Empathic Computing: Creating Shared Understanding
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Heart disease approach using modified random forest and particle swarm optimi...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Dev talk2

  • 1. ZOOX DEVTALK #2 PSR-7 E MIDDLEWARES JULHO / 2016
  • 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
  • 4. PSR7 - HEADERS $message = $message->withHeader('foo', 'bar'); echo $message->getHeaderLine('foo'); // bar echo $message->getHeaderLine('FOO'); // bar $message = $message->withHeader('fOO', 'baz'); echo $message->getHeaderLine('foo'); // baz $message = $message ->withHeader('foo', 'bar') ->withAddedHeader('foo', 'baz'); $header = $message->getHeaderLine('foo'); // 'bar, baz' $header = $message->getHeader('foo'); // ['bar', 'baz']
  • 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
  • 6. MIDDLEWARE - EXEMPLO <?php /** * Exemplo de um middleware * * @param PsrHttpMessageServerRequestInterface $request PSR7 request * @param PsrHttpMessageResponseInterface $response PSR7 response * @param callable $next Próximo * * @return PsrHttpMessageResponseInterface */ function ($request, $response, $next) { $response->getBody()->write('BEFORE'); $response = $next($request, $response); $response->getBody()->write('AFTER'); return $response; };
  • 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();
  • 9. ZEND EXPRESSIVE • Microframework voltada para PSR-7 middlewares • Recursos: • Routing • Container (container-interopt) • Templating • Error Handling • Pipelines
  • 10. EXPRESSIVE - EXEMPLO <?php use ZendExpressiveAppFactory; chdir(dirname(__DIR__)); require 'vendor/autoload.php'; $app = AppFactory::create(); $app->get('/', function ($request, $response, $next) { $response->getBody()->write('Hello, world!'); return $response; }); $app->pipeRoutingMiddleware(); $app->pipeDispatchMiddleware(); $app->run();