n1215/hakudo

A PSR7 / PSR-15 compatible HTTP router.

v0.7.0 2019-06-19 10:32 UTC

This package is auto-updated.

Last update: 2024-10-11 21:38:51 UTC


README

Latest Stable Version License Build Status Code Coverage Scrutinizer Code Quality

A PSR7 / PSR-15 compatible HTTP router using n1215/http-router, n1215/http-request-matcher and n1215/jugoya.

Usage

<?php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use N1215\Http\Router\Exception\RouteNotFoundException;
use N1215\Http\Router\Exception\RoutingException;
use N1215\Http\Router\Handler\RoutingErrorResponderInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;

// 1. Implement RoutingErrorResponderInterface.
class YourNotFoundErrorResponder implements RoutingErrorResponderInterface
{
    public function supports(RoutingException $exception): bool
    {
        // use this responder only when Route not found exception is thrown by the router.
        return $exception instanceof RouteNotFoundException;
    }
    
    public function respond(RoutingException $exception, ServerRequestInterface $request): ResponseInterface
    {
        // implement
    }
}

// 2. Implement PSR-11 Container
class YourContainer implements Psr\Container\ContainerInterface
{
    public function get($id)
    {
        // implement
    }

    public function has($id) 
    {
        // implement
    }
}

// 3. Implement PSR-15 RequestHandler
class YourHandler implements RequestHandlerInterface
{
    public function handle(ServerRequestInterface $request): ResponseInterface 
    {
        $id = $request->getAttribute('id'); // matched route parameters becomes attributes.
        // implement
    }
}

// 4. Implement PSR-15 Middleware
class YourMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // implement
    }
}

// 5. register handler and middleware to the container
$container = new YourContainer();
$container->bind(YourHandler::class, function () { return new YourHandler(); });
$container->bind(YourMiddleware::class, function () { return new YourMiddleware(); });

// 6. create Router instance
$router = new N1215\Hakudo\Router(
    N1215\Jugoya\LazyRequestHandlerBuilder::fromContainer($container),
    new N1215\Http\Router\Result\RoutingResultFactory()
);

// 7. add routes
use N1215\Hakudo\RequestMatcher\Path;
$router->add(
    Path::get('|/resources/(?<resource_name>[a-z_]+)/(?<id>[0-9]+)|'), // you can use any other RequestMatcher instance.
    YourHandler::class, // you can use same signature closure or RequestHandler instance instead of RequestHandler's container entry name.
    [YourMiddleware::class] // you can use same signature closure or Middleware instance instead of Middleware's container entry name.
)->name('route_name');

// 8. create RoutingHandler
$routingHandler = new N1215\Http\Router\Handler\RoutingHandler(
    $router,
    new YourNotFoundErrorResponder()
);

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

Class diagrams

Hakudo

hakudo

RouterInterface

n1215/http-router

RequestMatcherInterface

n1215/http-request-matcher

Jugoya

n1215/jugoya

License

The MIT License (MIT). Please see LICENSE for more information.