PHP code example of shadowhand / either-way

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

    

shadowhand / either-way example snippets


$request = $serverRequestFactory->createServerRequest($_SERVER);

$dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
    $r->get('/[{name}]', Acme\WelcomeController::class);
});

$handleError = function (int $httpStatus) use ($responseFactory): ResponseInterface {
    return $responseFactory->createResponse($httpStatus);
};

use EitherWay\Route;

$handleSuccess = function (Route $route) use ($container): ResponseInterface {
    $handler = $container->get($route->handler());
    $response = $handler($route->request());

    return $response;
};

use function EitherWay\dispatch;

$response = dispatch($request, $dispatcher)
    ->either($handleError, $handleSuccess);