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?

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

  • 1.
    Introducción a aplicacionesPHP basadas en Middleware y PSR-7 Alejandro Celaya  —  @acelayaa
  • 2.
    PHP Framework InteropGroup Proponen recomendaciones de estándar. PSR-7 - HTTP Messages PSR-15 - Server middleware PHP-FIG
  • 3.
    PSR-7 - HTTPMessages 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 procesode 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.
  • 7.
    Cache middleware public CacheMiddlewareimplements 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 RoutingMiddlewareimplements 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 AuthorizationMiddlewareimplements 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 enmiddleware Zend Expressive (Stratigility) Slim framework v3 Stack PHP Relay PHP Spiral framework Middleman
  • 11.
    Enlaces de interés Expressive: Slim: Stack: PHP-FIG: PSR-7spec: 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
  • 12.
  • 13.