PHP code example of httpsoft / http-runner

1. Go to this page and download the library: Download httpsoft/http-runner 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/ */

    

httpsoft / http-runner example snippets


use HttpSoft\Runner\MiddlewarePipeline;
use HttpSoft\Runner\MiddlewareResolver;
use HttpSoft\Runner\ServerRequestRunner;
use Psr\Http\Message\ResponseInterface;

/**
 * @var Psr\Http\Message\ServerRequestInterface $request
 * @var Psr\Http\Server\RequestHandlerInterface $handler
 * @var Psr\Http\Server\MiddlewareInterface $siteMiddleware
 * @var Psr\Http\Server\MiddlewareInterface $userMiddleware
 */

// Basic usage

$runner = new ServerRequestRunner();
$runner->run($request, $handler);

// Using `MiddlewarePipeline`

$pipeline = new MiddlewarePipeline();
$pipeline->pipe($siteMiddleware);
$pipeline->pipe($userMiddleware, '/user');

$runner = new ServerRequestRunner($pipeline);
$runner->run($request, $handler);

// Using `MiddlewareResolver`

$resolver = new MiddlewareResolver();
$handlerMiddleware = $resolver->resolve(function (): ResponseInterface {
    $response = new HttpSoft\Message\Response();
    $response->getBody()->write('Hello world!');
    return $response;
});

$pipeline = new MiddlewarePipeline();
$pipeline->pipe($siteMiddleware);
$pipeline->pipe($userMiddleware, '/user');
$pipeline->pipe($handlerMiddleware);

$runner = new ServerRequestRunner($pipeline);
$runner->run($request); // Output result: 'Hello world!'