Created
November 17, 2016 19:22
-
-
Save geerteltink/7b90127d3811e65f9fcaf7b28f6b98b9 to your computer and use it in GitHub Desktop.
Expressive and PSR-15 interfaces for HTTP middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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