alexpts/psr15-next

Router for PSR-7 and PSR-15

0.5.0 2020-12-01 19:27 UTC

This package is auto-updated.

Last update: 2024-11-29 05:26:56 UTC


README

phpunit codecov

Runner for PSR-15 middlewares.

See examples

  • Named routes with URL generation
  • Responds to OPTIONS requests with allowed methods
  • Multiple route middleware
  • Multiple routers
  • Nestable routers
  • PSR-15 middlewares
  • PSR-7 request/response
  • Flexible priority (low level)
  • Dynamic endpoint (low level)
use Psr\Http\Message\ServerRequestInterface;
use PTS\NextRouter\Resolver\LayerResolver;
use PTS\NextRouter\Next;
use PTS\PSR15\Middlewares\ErrorToJsonResponse;
use PTS\ParserPsr7\SapiEmitter;
use PTS\Psr7\Factory\Psr17Factory;
use PTS\Psr7\Response\JsonResponse;

require_once '../vendor/autoload.php';

$psr17Factory = new Psr17Factory;
$app = new Next;

$app->getRouterStore()
    ->get('/hello', function (ServerRequestInterface $request, $next) {
        return new JsonResponse(['message' => 'Hello world'], 200);
    })
    ->use(function (ServerRequestInterface $request, $next) {
        return new JsonResponse(['message' => 'otherwise']);
    });

$request = $psr17Factory->fromGlobals();
$response = $app->handle($request);
(new SapiEmitter)->emit($response);

Install

composer require alexpts/psr15-next

Todo: add more examples