2017 © Pedro Peláez
 

library either-way

A functional route dispatcher

image

shadowhand/either-way

A functional route dispatcher

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

EitherWay

EitherWay combines the excellent FastRoute with FP-Either to create an easy to dispatch routes that resolve to class names or container identifiers., (*1)

Installation

composer require shadowhand/either-way

Usage

First, create a PSR-7 ServerRequestInterface. In this example, we will use PSR-17 ServerRequestFactory., (*2)

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

Next, create a Dispatcher with FastRoute:, (*3)

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

Now define two handlers: one to handle routing errors, and one to handle successful routing:, (*4)

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

Note that the error value will be an HTTP status code. How this code is mapped to a response is up to you, the only requirement is that the error handler will return a PSR-7 ResponseInterface., (*5)

use EitherWay\Route;

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

    return $response;
};

The EitherWay Route contains two values: the handler string, which is either a class name or a container identifier, and the server request with route parameters attached to it., (*6)

Again, how the handler and the request get mapped to a response is up to you, the only requirement is that the handler returns a response., (*7)

Dispatching

Now that all everything is defined, we can execute the routing:, (*8)

use function EitherWay\dispatch;

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

At this point, the response can modified and ultimately sent., (*9)

License

MIT, (*10)

The Versions

17/03 2017

dev-master

9999999-dev

A functional route dispatcher

  Sources   Download

MIT

The Requires

 

The Development Requires

17/03 2017

1.0.0

1.0.0.0

A functional route dispatcher

  Sources   Download

MIT

The Requires

 

The Development Requires