PHP code example of cormy / server-middleware

1. Go to this page and download the library: Download cormy/server-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cormy / server-middleware example snippets


use Generator;
use Cormy\Server\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class Middleware implements MiddlewareInterface
{
    /**
     * Process an incoming server request and return the response, optionally delegating
     * to the next request handler.
     *
     * @param ServerRequestInterface $request
     *
     * @return Generator yields PSR `ServerRequestInterface` instances and returns a PSR `ResponseInterface` instance
     */
    public function __invoke(ServerRequestInterface $request):Generator
    {
        // delegate $request to the next request handler
        $response = yield $request;

        // mofify the response
        $response = $response->withHeader('X-PoweredBy', 'Unicorns');

        return $response;
    }
}