Giter Club home page Giter Club logo

micro-router's Introduction

Micro Router

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.

micro-router's People

Contributors

chi-teck avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.