Skip to content

Instantly share code, notes, and snippets.

@geerteltink
Created November 17, 2016 19:22
Show Gist options
  • Save geerteltink/7b90127d3811e65f9fcaf7b28f6b98b9 to your computer and use it in GitHub Desktop.
Save geerteltink/7b90127d3811e65f9fcaf7b28f6b98b9 to your computer and use it in GitHub Desktop.
Expressive and PSR-15 interfaces for HTTP middleware
<?php
namespace App\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Interop\Http\Middleware\ServerMiddlewareInterface;
use Interop\Http\Middleware\DelegateInterface;
/**
* HTTP Middleware
*
* PSR-15 interfaces for HTTP middleware.
*/
class MyMiddleware implements ServerMiddlewareInterface
{
public function __construct()
{
}
/**
* @param Request $request
* @param DelegateInterface $delegate
*
* @return Response
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface
{
// Do something (with the request) first
// Call the next middleware and wait for the response
$response = $delegate->process($request);
// Do something (with the response) before returning the response
// Return the response
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment