SlideShare une entreprise Scribd logo
1  sur  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();

Contenu connexe

En vedette

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
 

En vedette (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
 

Similaire à 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
 

Similaire à 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)
 

Dernier

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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
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
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Dernier (20)

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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
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
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

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