chi-teck/micro-router

1.0.0-beta2 2021-01-22 19:37 UTC

This package is auto-updated.

Last update: 2024-11-23 04:05:19 UTC


README

A simple and fast routing system for PSR-7 requests.

Requirements

Installation

composer require chi-teck/micro-router

Usage

Define routes

$routes = new RouteCollection();

$routes['article.view'] = new Route(
    methods: ['GET'],
    path: '/article/{id}',
    requirements: ['id' => '\d+'],
    handler: ArticleViewHandler::class,
);

$routes['article.update'] = new Route(
    methods: ['PUT'],
    path: '/article/{id}',
    requirements: ['id' => '\d+'],
    handler: ArticleUpdateHandler::class,
);

$routes['article.delete'] = new Route(
    methods: ['DELETE'],
    path: '/article/{id}',
    requirements: ['id' => '\d+'],
    handler: ArticleDeleteHandler::class,
);

$routes['article.create'] = new Route(
    methods: ['POST'],
    path: '/article',
    handler: ArticleCreateHandler::class,
);

Alternatively, the routes can be defined via create factory method.

$routes = new RouteCollection();
$routes['article.view'] = Route::create('GET', '/article/{id:\d+}', ArticleViewHandler::class);
$routes['article.update'] = Route::create('PUT', '/article/{id:\d+}', ArticleUpdateHandler::class);
$routes['article.delete'] = Route::create('DELETE', '/article/{id:\d+}', ArticleDeleteHandler::class);
$routes['article.create'] = Route::create('POST', '/article', ArticleCreateHandler::class);

Handle request

use MicroRouter\Contract\Exception\MethodNotAllowedInterface;
use MicroRouter\Contract\Exception\ResourceNotFoundInterface;
use MicroRouter\Matcher;

/** @var \MicroRouter\Contract\RouteCollectionInterface $routes */
$routes = require __DIR__ . '/path/to/routes.php';

/** @var \Psr\SimpleCache\CacheInterface $cache */
$matcher = Matcher::create($cache);

/** @var \Psr\Http\Message\ServerRequestFactoryInterface $request_factory */
// In real application the request is created from PHP super globals.
$request = $request_factory->createServerRequest('GET', '/article/123');
try {
    $result = $matcher->match($request, $routes);
    $handler = $result->getRoute()->getHandler();
    // Depending on handler type (closure, service ID, etc) you may need to
    // resolve the callable before invoking.
    $response = $handler(...$result->getParameters());
}
catch (ResourceNotFoundInterface) {
    /** @var \Psr\Http\Message\ResponseFactoryInterface $response_factory */
    $response = $response_factory->createResponse(404);
}
catch (MethodNotAllowedInterface $exception) {
    /** @var \Psr\Http\Message\ResponseFactoryInterface $response_factory */
    $response = $response_factory->createResponse(405)
        ->withHeader('Allowed', $exception->getAllowedMethods());
}

License

MIT License.