PHP code example of codin / relay

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

    

codin / relay example snippets


use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

// Define some Middlewares
$response = new class() implements MiddlewareInterface {
    public function process(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
        $psr17Factory = new Nyholm\Psr7\Factory\Psr17Factory();
        $responseBody = $psr17Factory->createStream('');
        return $psr17Factory->createResponse(200)->withBody($responseBody);
    }
};

$cors = new class() implements MiddlewareInterface {
    public function process(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
        return $handler->handle($request)->withHeader('Access-Control-Allow-Origin', '*');
    }
};

$exceptions = new class() implements MiddlewareInterface {
    public function process(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
        try {
            return $handler->handle($request);
        } catch (Throwable $exception) {
            $this->logger->error('Server Error', compact('exception'));
            $psr17Factory = new Nyholm\Psr7\Factory\Psr17Factory();
            $responseBody = $psr17Factory->createStream('Server Error');
            return $psr17Factory->createResponse(500)->withBody($responseBody);
        }
    }
};

// Create a request
$psr17Factory = new Nyholm\Psr7\Factory\Psr17Factory();
$request = $psr17Factory->createRequest('GET', 'http://tnyholm.se');

// Run middlewares on a request to create a response
$relay = new Codin\Relay\Relay([$cors, $exceptions, $response]);
$response = $relay->handle($request);