SlideShare a Scribd company logo
Introducción a aplicaciones PHP
basadas en Middleware y PSR-7
Alejandro Celaya  —  @acelayaa
PHP Framework Interop Group
Proponen recomendaciones de estándar.
PSR-7 - HTTP Messages
PSR-15 - Server middleware
PHP-FIG
PSR-7 - HTTP Messages
El estándar HTTP de ne dos tipos de mensajes:
Request y Response
PHP está orientado hacia la web, y usa HTTP
PSR-7 propone que Request y Response sean
inmutables
Middleware
Gestión del proceso de resolución de un request a
través de capas o middlewares.
Cada middleware decide si invocar al siguiente en la
lista.
Firmas del middleware
Double-pass: Request + Response
Single pass: Request (PSR-15)
El response debería modi carse sólo en el camino de
salida.
public function __invoke(
Request $reqest,
Response $response,
callable $next
): Response;
public function process(Request $reqest, Delegate $next) : Response;
Arquitectura middleware
Imágen de Zend Expressive Docs
Cache middleware
public CacheMiddleware implements MiddlewareInterface
{
public function process(RequestInterface $request, DelegateInterface $delegate)
{
// If this route is already cached, return cached content
$cacheKey = (string) $request->getUri();
if ($this->cache->contains($cacheKey)) {
$body = new Stream('php://temp', 'rw');
$body->write($this->cache->fetch($cacheKey))
return (new Response())->withBody($body);
}
// If this route has not been cached yet, process it and cache the result
$resp = $delegate->process($request);
$this->cache->save($cacheKey, (string) $resp->getBody());
return $resp;
}
}
Routing middleware
public RoutingMiddleware implements MiddlewareInterface
{
public function process(Request $request, DelegateInterface $delegate)
{
$route = $this->router->match($request);
// If no route was found, return not found error
if ($route === null) {
return (new Response())->withStatus(404);
}
$request = $request->withAttribute('route', $route);
return $delegate->process($request);
}
}
Authorization middleware
public AuthorizationMiddleware implements MiddlewareInterface
{
public function process(Request $request, DelegateInterface $delegate)
{
$route = $request->getAttribute('route');
$resource = $route->getRouteName();
$role = $this->determineCurrentUserRole();
// If the role is allowed to access the resource, process next middleware
if ($this->acl->isAllowed($role, $resource)) {
return $delegate->process($request);
}
// If the role is not allowed to access the resource, return a forbidden response
return (new Response())->withStatus(403);
}
}
Frameworks basados en middleware
Zend Expressive (Stratigility)
Slim framework v3
Stack PHP
Relay PHP
Spiral framework
Middleman
Enlaces de interés
Expressive:
Slim:
Stack:
PHP-FIG:
PSR-7 spec:
PSR-15 spec:
PSR-7 middleware:
PSR-15 middleware:
https://docs.zendframework.com/zend-expressive/
https://www.slimframework.com/
http://stackphp.com/
http://www.php- g.org/
http://www.php- g.org/psr/psr-7/
https://github.com/php- g/ g-
standards/blob/master/proposed/http-middleware/middleware.md
https://github.com/oscarotero/psr7-middlewares
https://github.com/middlewares/awesome-psr15-
middlewares
Conclusión
¿Preguntas?

More Related Content

Similar to Introducción a aplicaciones php basadas en middleware y psr 7 (20)

PDF
Make your application expressive
Christian Varela
 
PDF
Native REST Web Services with Oracle 11g
Marcelo Ochoa
 
PPT
Node.js Express Framework
TheCreativedev Blog
 
PPTX
express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
rani marri
 
DOCX
Servlet
Dhara Joshi
 
PDF
Zend/Expressive 3 – The Next Generation
Ralf Eggert
 
ODP
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
King Foo
 
PPTX
Rapid API development examples for Impress Application Server / Node.js (jsfw...
Timur Shemsedinov
 
PDF
ExpressJS-Introduction.pdf
Bareen Shaikh
 
PPTX
Service approach for development Rest API in Symfony2
Sumy PHP User Grpoup
 
PDF
Express: A Jump-Start
Naveen Pete
 
PDF
Hello World on Slim Framework 3.x
Ryan Szrama
 
PPTX
Rits Brown Bag - PHP & PHPMyAdmin
Right IT Services
 
PPTX
Performance #4 network
Vitali Pekelis
 
PPTX
ExpressJS and REST API.pptx
Govardhan Bhavani
 
PPTX
Overview of RESTful web services
nbuddharaju
 
PPT
Rapid java backend and api development for mobile devices
ciklum_ods
 
PDF
Criando uma API com Zend Expressive 3
Juciellen Cabrera
 
PDF
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
Joshua Long
 
Make your application expressive
Christian Varela
 
Native REST Web Services with Oracle 11g
Marcelo Ochoa
 
Node.js Express Framework
TheCreativedev Blog
 
express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
rani marri
 
Servlet
Dhara Joshi
 
Zend/Expressive 3 – The Next Generation
Ralf Eggert
 
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
King Foo
 
Rapid API development examples for Impress Application Server / Node.js (jsfw...
Timur Shemsedinov
 
ExpressJS-Introduction.pdf
Bareen Shaikh
 
Service approach for development Rest API in Symfony2
Sumy PHP User Grpoup
 
Express: A Jump-Start
Naveen Pete
 
Hello World on Slim Framework 3.x
Ryan Szrama
 
Rits Brown Bag - PHP & PHPMyAdmin
Right IT Services
 
Performance #4 network
Vitali Pekelis
 
ExpressJS and REST API.pptx
Govardhan Bhavani
 
Overview of RESTful web services
nbuddharaju
 
Rapid java backend and api development for mobile devices
ciklum_ods
 
Criando uma API com Zend Expressive 3
Juciellen Cabrera
 
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
Joshua Long
 

Recently uploaded (20)

PDF
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Ad

Introducción a aplicaciones php basadas en middleware y psr 7

  • 1. Introducción a aplicaciones PHP basadas en Middleware y PSR-7 Alejandro Celaya  —  @acelayaa
  • 2. PHP Framework Interop Group Proponen recomendaciones de estándar. PSR-7 - HTTP Messages PSR-15 - Server middleware PHP-FIG
  • 3. PSR-7 - HTTP Messages El estándar HTTP de ne dos tipos de mensajes: Request y Response PHP está orientado hacia la web, y usa HTTP PSR-7 propone que Request y Response sean inmutables
  • 4. Middleware Gestión del proceso de resolución de un request a través de capas o middlewares. Cada middleware decide si invocar al siguiente en la lista.
  • 5. Firmas del middleware Double-pass: Request + Response Single pass: Request (PSR-15) El response debería modi carse sólo en el camino de salida. public function __invoke( Request $reqest, Response $response, callable $next ): Response; public function process(Request $reqest, Delegate $next) : Response;
  • 6. Arquitectura middleware Imágen de Zend Expressive Docs
  • 7. Cache middleware public CacheMiddleware implements MiddlewareInterface { public function process(RequestInterface $request, DelegateInterface $delegate) { // If this route is already cached, return cached content $cacheKey = (string) $request->getUri(); if ($this->cache->contains($cacheKey)) { $body = new Stream('php://temp', 'rw'); $body->write($this->cache->fetch($cacheKey)) return (new Response())->withBody($body); } // If this route has not been cached yet, process it and cache the result $resp = $delegate->process($request); $this->cache->save($cacheKey, (string) $resp->getBody()); return $resp; } }
  • 8. Routing middleware public RoutingMiddleware implements MiddlewareInterface { public function process(Request $request, DelegateInterface $delegate) { $route = $this->router->match($request); // If no route was found, return not found error if ($route === null) { return (new Response())->withStatus(404); } $request = $request->withAttribute('route', $route); return $delegate->process($request); } }
  • 9. Authorization middleware public AuthorizationMiddleware implements MiddlewareInterface { public function process(Request $request, DelegateInterface $delegate) { $route = $request->getAttribute('route'); $resource = $route->getRouteName(); $role = $this->determineCurrentUserRole(); // If the role is allowed to access the resource, process next middleware if ($this->acl->isAllowed($role, $resource)) { return $delegate->process($request); } // If the role is not allowed to access the resource, return a forbidden response return (new Response())->withStatus(403); } }
  • 10. Frameworks basados en middleware Zend Expressive (Stratigility) Slim framework v3 Stack PHP Relay PHP Spiral framework Middleman
  • 11. Enlaces de interés Expressive: Slim: Stack: PHP-FIG: PSR-7 spec: PSR-15 spec: PSR-7 middleware: PSR-15 middleware: https://docs.zendframework.com/zend-expressive/ https://www.slimframework.com/ http://stackphp.com/ http://www.php- g.org/ http://www.php- g.org/psr/psr-7/ https://github.com/php- g/ g- standards/blob/master/proposed/http-middleware/middleware.md https://github.com/oscarotero/psr7-middlewares https://github.com/middlewares/awesome-psr15- middlewares