PHP code example of tourze / psr15-chain-request-handler
1. Go to this page and download the library: Download tourze/psr15-chain-request-handler 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/ */
tourze / psr15-chain-request-handler example snippets
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use Tourze\PSR15ChainRequestHandler\ChainRequestHandler;
use Psr\Http\Server\RequestHandlerInterface;
// Example handler that always returns 404
class NotFoundHandler implements RequestHandlerInterface {
public function handle($request): Response {
return new Response(404, body: 'Not Found');
}
}
// Example handler that returns 200 for a specific path
class HelloHandler implements RequestHandlerInterface {
public function handle($request): Response {
if ($request->getUri()->getPath() === '/hello') {
return new Response(200, body: 'Hello World');
}
return new Response(404, body: 'Not Found');
}
}
$chain = new ChainRequestHandler([
new NotFoundHandler(),
new HelloHandler(),
]);
$request = new ServerRequest('GET', '/hello');
$response = $chain->handle($request);
// $response->getStatusCode() === 200