PHP code example of cormy / server-middleware-dispatcher

1. Go to this page and download the library: Download cormy/server-middleware-dispatcher 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-dispatcher example snippets


use Cormy\Server\MiddlewareDispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

$middleware = function (ServerRequestInterface $request):\Generator {
    // delegate $request to the next request handler, i.e. the $finalHandler below
    $response = (yield $request);

    return $response->withHeader('X-PoweredBy', 'Unicorns');
};

$finalHandler = function (ServerRequestInterface $request):ResponseInterface {
    return new \Zend\Diactoros\Response();
};

// create a dispatcher
$dispatcher = new MiddlewareDispatcher($middleware, $finalHandler);

// dispatch a request
$response = $dispatcher(new \Zend\Diactoros\ServerRequest());

/**
 * Create a Cormy PSR-7 server middleware dispatcher.
 *
 * @param callable|MiddlewareInterface     $middleware
 * @param callable|RequestHandlerInterface $finalHandler
 */
public function __construct(callable $middleware, callable $finalHandler)

/**
 * Process an incoming server request and return the response.
 *
 * @param ServerRequestInterface $request
 *
 * @return ResponseInterface
 */
public function __invoke(ServerRequestInterface $request):ResponseInterface;