SlideShare una empresa de Scribd logo
1 de 10
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();

Más contenido relacionado

Destacado

Web Designing Company in India
Web Designing Company in IndiaWeb Designing Company in India
Web Designing Company in IndiaJagbir Singh Kang
 
EDA DE PAZ
EDA DE PAZ EDA DE PAZ
EDA DE PAZ Eda_123
 
International Journal of Computational Science and Information Technology (I...
 International Journal of Computational Science and Information Technology (I... International Journal of Computational Science and Information Technology (I...
International Journal of Computational Science and Information Technology (I...ijcsity
 
2015 CIC: #EdTech Forum - Understanding and Influencing Publishing Standards
2015 CIC: #EdTech Forum - Understanding and Influencing Publishing Standards2015 CIC: #EdTech Forum - Understanding and Influencing Publishing Standards
2015 CIC: #EdTech Forum - Understanding and Influencing Publishing StandardsAAP PreK-12 Learning Group
 
Argumente per rrugen e drejte
Argumente per rrugen e drejteArgumente per rrugen e drejte
Argumente per rrugen e drejteendrit endri
 
La Commission Interaméricaine des Droits de l'Homme constate la vacance Prési...
La Commission Interaméricaine des Droits de l'Homme constate la vacance Prési...La Commission Interaméricaine des Droits de l'Homme constate la vacance Prési...
La Commission Interaméricaine des Droits de l'Homme constate la vacance Prési...Stanleylucas
 
MyDailyChoice Opportunity Presentation
MyDailyChoice Opportunity PresentationMyDailyChoice Opportunity Presentation
MyDailyChoice Opportunity PresentationGideon Adebisi
 
Grecia antigua periodo pericles
Grecia antigua  periodo pericles Grecia antigua  periodo pericles
Grecia antigua periodo pericles Lucía Chavez
 
Solving linear trig equations
Solving linear trig equationsSolving linear trig equations
Solving linear trig equationsDawn Adams2
 
Lights, Camera, Advocacy: Digital Storytelling for Your Library
Lights, Camera, Advocacy: Digital Storytelling for Your LibraryLights, Camera, Advocacy: Digital Storytelling for Your Library
Lights, Camera, Advocacy: Digital Storytelling for Your LibraryCrystal Schimpf
 
CdP Renault bilan 2010_2013
 CdP Renault bilan 2010_2013 CdP Renault bilan 2010_2013
CdP Renault bilan 2010_2013Fing
 
10 TERMETET DHE 10 VULLKANET ME TE MEDHENJ NE BOTE
10 TERMETET DHE 10 VULLKANET ME TE MEDHENJ NE BOTE10 TERMETET DHE 10 VULLKANET ME TE MEDHENJ NE BOTE
10 TERMETET DHE 10 VULLKANET ME TE MEDHENJ NE BOTEGersa_a
 
Arte griego
Arte griegoArte griego
Arte griegoPaula Fs
 

Destacado (15)

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

Similar a Dev talk2

Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Ajay Khatri
 
php_postgresql.ppt
php_postgresql.pptphp_postgresql.ppt
php_postgresql.pptElieNGOMSEU
 
PHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connectingPHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connectingPraveenHegde20
 
Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....Lalith86
 
PSR-7 - HTTP message interfaces
PSR-7 - HTTP message interfacesPSR-7 - HTTP message interfaces
PSR-7 - HTTP message interfacesThe Software House
 
PHP5.3 を使うのはやめよう
PHP5.3 を使うのはやめようPHP5.3 を使うのはやめよう
PHP5.3 を使うのはやめようy-uti
 
Submit PHP: Standards in PHP world. Михайло Морозов
Submit PHP: Standards in PHP world. Михайло МорозовSubmit PHP: Standards in PHP world. Михайло Морозов
Submit PHP: Standards in PHP world. Михайло МорозовBinary Studio
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHPNat Weerawan
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners musrath mohammad
 
php (Hypertext Preprocessor)
php (Hypertext Preprocessor)php (Hypertext Preprocessor)
php (Hypertext Preprocessor)Chandan Das
 
Ch1(introduction to php)
Ch1(introduction to php)Ch1(introduction to php)
Ch1(introduction to php)Chhom Karath
 

Similar a Dev talk2 (18)

Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Introduction to PHP Lecture 1
Introduction to PHP Lecture 1
 
php_postgresql.ppt
php_postgresql.pptphp_postgresql.ppt
php_postgresql.ppt
 
PHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connectingPHP with Postgres SQL connection string and connecting
PHP with Postgres SQL connection string and connecting
 
Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....Introduction to php and POSTGRESQL. ....
Introduction to php and POSTGRESQL. ....
 
php_postgresql.ppt
php_postgresql.pptphp_postgresql.ppt
php_postgresql.ppt
 
PSR-7 - HTTP message interfaces
PSR-7 - HTTP message interfacesPSR-7 - HTTP message interfaces
PSR-7 - HTTP message interfaces
 
Php performance
Php performancePhp performance
Php performance
 
PHP5.3 を使うのはやめよう
PHP5.3 を使うのはやめようPHP5.3 を使うのはやめよう
PHP5.3 を使うのはやめよう
 
Submit PHP: Standards in PHP world. Михайло Морозов
Submit PHP: Standards in PHP world. Михайло МорозовSubmit PHP: Standards in PHP world. Михайло Морозов
Submit PHP: Standards in PHP world. Михайло Морозов
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHP
 
Intro to PHP
Intro to PHPIntro to PHP
Intro to PHP
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
 
PHP
PHPPHP
PHP
 
php (Hypertext Preprocessor)
php (Hypertext Preprocessor)php (Hypertext Preprocessor)
php (Hypertext Preprocessor)
 
Key features PHP 5.3 - 5.6
Key features PHP 5.3 - 5.6Key features PHP 5.3 - 5.6
Key features PHP 5.3 - 5.6
 
Ch1(introduction to php)
Ch1(introduction to php)Ch1(introduction to php)
Ch1(introduction to php)
 

Último

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 

Último (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

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();