n1215/http-router

A PSR-7 / PSR-15 compatible router interface and a PSR-15 server request handler implementation for routing.

v2.0.0 2019-06-19 10:25 UTC

This package is auto-updated.

Last update: 2024-10-11 21:06:13 UTC


README

Latest Stable Version License Build Status Code Coverage Scrutinizer Code Quality

A PSR-7 / PSR-15 compatible router interface and a PSR-15 server request handler implementation for routing.

This package helps you to create your own http router.

Usage

<?php

// 1. Implement RouterInterface. You can use RoutingResultFactory concrete classes to create RoutingResult.
class YourRouter implements N1215\Http\Router\RouterInterface
{
    public function match(ServerRequestInterface $request) : RoutingResultInterface
    {
        // implement
    }
}

// 2. Implement RoutingErrorResponderInterface.
class YourRoutingErrorResponder implements N1215\Http\Router\Handler\RoutingErrorResponderInterface
{
    public function supports(RoutingException $exception): bool
    {
        // implement
    }

    public function respond(RoutingException $exception, ServerRequestInterface $request): ResponseInterface
    {
        // implement
    }
}

// 3. Configure to inject them into RoutingHandler.
$routingHandler = new N1215\Http\Router\Handler\RoutingHandler(
    new YourRouter(),
    new YourNotFoundErrorResponder(),
    new YourMethodNotAllowedErrorResponder()
);

// 4. Use RoutingHandler as an implementation of PSR-15 server request handler.
/** @var \Psr\Http\Message\ServerRequestInterface $request */
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $routingHandler->handle($request);

Implementation examples

Class diagrams

RouterInterface

router

RoutingException and RoutingErrorResponderInterface

routing-error

RoutingHandler

routing-handler