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

PDF
Stacking Up Middleware
PDF
Zend Expressive 3 e PSR-15
PDF
PSR-7, middlewares e o futuro dos frameworks
PPTX
Dev talk2
PDF
PDF
Building web APIs in PHP with Zend Expressive
PDF
PSR-7 - HTTP message interfaces
PDF
Developing web APIs using middleware in PHP 7
Stacking Up Middleware
Zend Expressive 3 e PSR-15
PSR-7, middlewares e o futuro dos frameworks
Dev talk2
Building web APIs in PHP with Zend Expressive
PSR-7 - HTTP message interfaces
Developing web APIs using middleware in PHP 7

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

PDF
Future of HTTP in CakePHP
PDF
Make your application expressive
PDF
Zend/Expressive 3 – The Next Generation
PDF
Criando uma API com Zend Expressive 3
PDF
PSR-7 and PSR-15, why can't you ignore them
PDF
Submit PHP: Standards in PHP world. Михайло Морозов
PDF
Develop web APIs in PHP using middleware with Expressive (Code Europe)
PPT
PHP Frameworks in 2015 - The rise of middleware
PDF
Meet up symfony 16 juin 2017 - Les PSR
PDF
Psr 7 symfony-day
PDF
Middleware PHP - A simple micro-framework
PDF
Php and webservices
PDF
PSR7 - interoperabilità HTTP
PDF
Building Restful Applications Using Php
PDF
Develop microservices in php
PDF
Web services tutorial
PPTX
Zend Expressive in 15 Minutes
PDF
Adding Real-time Features to PHP Applications
PDF
Php and-web-services-24402
PDF
Php And Web Services
Future of HTTP in CakePHP
Make your application expressive
Zend/Expressive 3 – The Next Generation
Criando uma API com Zend Expressive 3
PSR-7 and PSR-15, why can't you ignore them
Submit PHP: Standards in PHP world. Михайло Морозов
Develop web APIs in PHP using middleware with Expressive (Code Europe)
PHP Frameworks in 2015 - The rise of middleware
Meet up symfony 16 juin 2017 - Les PSR
Psr 7 symfony-day
Middleware PHP - A simple micro-framework
Php and webservices
PSR7 - interoperabilità HTTP
Building Restful Applications Using Php
Develop microservices in php
Web services tutorial
Zend Expressive in 15 Minutes
Adding Real-time Features to PHP Applications
Php and-web-services-24402
Php And Web Services
Ad

Recently uploaded (20)

PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
AI in Product Development-omnex systems
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Nekopoi APK 2025 free lastest update
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
history of c programming in notes for students .pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
System and Network Administraation Chapter 3
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
System and Network Administration Chapter 2
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Navsoft: AI-Powered Business Solutions & Custom Software Development
How Creative Agencies Leverage Project Management Software.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
AI in Product Development-omnex systems
Understanding Forklifts - TECH EHS Solution
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Nekopoi APK 2025 free lastest update
CHAPTER 2 - PM Management and IT Context
Softaken Excel to vCard Converter Software.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
history of c programming in notes for students .pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
System and Network Administraation Chapter 3
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
System and Network Administration Chapter 2
PTS Company Brochure 2025 (1).pdf.......
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
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