All Projects → northwoods → router

northwoods / router

Licence: other
Fast router for PSR-15 request handlers

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to router

Examples
Examples of Mock Service Worker usage with various frameworks and libraries.
Stars: ✭ 163 (+805.56%)
Mutual labels:  handler, request
tiny-qiniu-request
tiny-qiniu for rc-upload or antd upload component `customRequest` property
Stars: ✭ 13 (-27.78%)
Mutual labels:  request
AxleJS
Fetch, supercharged.
Stars: ✭ 28 (+55.56%)
Mutual labels:  request
BookingScraper
🌎 🏨 Scrape Booking.com 🏨 🌎
Stars: ✭ 68 (+277.78%)
Mutual labels:  request
chronosjs
JS Channels (Events / Commands / Reqest-Response / Courier) Mechanism
Stars: ✭ 35 (+94.44%)
Mutual labels:  request
oh-my-request
🔮 simple request library by java8
Stars: ✭ 41 (+127.78%)
Mutual labels:  request
Middleware
Community Middleware List for the Iris Web Framework.
Stars: ✭ 188 (+944.44%)
Mutual labels:  handler
monolog-telegram-handler
Monolog handler to send log by Telegram
Stars: ✭ 32 (+77.78%)
Mutual labels:  handler
get-it
Composable HTTP request library for node and browsers
Stars: ✭ 19 (+5.56%)
Mutual labels:  request
httper
An asynchronous HTTP(S) client built on top of hyper.
Stars: ✭ 16 (-11.11%)
Mutual labels:  request
node-match-path
Matches a URL against a path. Parameters, wildcards, RegExp.
Stars: ✭ 30 (+66.67%)
Mutual labels:  request
http-method
🍇 Provides constants for HTTP request methods.
Stars: ✭ 83 (+361.11%)
Mutual labels:  request
defense
🔮 A Crystal HTTP handler for throttling, blocking and tracking malicious requests.
Stars: ✭ 51 (+183.33%)
Mutual labels:  handler
reqres
Powerful classes for http requests and responses
Stars: ✭ 36 (+100%)
Mutual labels:  request
nestjs-cls
A continuation-local storage (async context) module compatible with NestJS's dependency injection.
Stars: ✭ 110 (+511.11%)
Mutual labels:  request
Alice
Painless middleware chaining for Go
Stars: ✭ 2,438 (+13444.44%)
Mutual labels:  handler
hent
A small utility to fetch remote files into buffers
Stars: ✭ 23 (+27.78%)
Mutual labels:  request
request-on-steroids
An HTTP client ✨ with retry, circuit-breaker and tor support 📦 out-of-the-box
Stars: ✭ 19 (+5.56%)
Mutual labels:  request
jest-playback
Record and playback http requests from your Jest tests
Stars: ✭ 51 (+183.33%)
Mutual labels:  request
JYSort
Android 图形化排序, 六种类排序算法可视化展示
Stars: ✭ 15 (-16.67%)
Mutual labels:  handler

Northwoods Router

Build Status Code Quality Code Coverage Latest Stable Version Total Downloads License

A FastRoute based router designed to be used with PSR-15 middleware.

Installation

The best way to install and use this package is with composer:

composer require northwoods/router

Usage

The router implements MiddlewareInterface and can be used with any middleware dispatcher, such as Broker.

use Northwoods\Router\Router;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

$router = new Router();
$router->get('user.list', '/users', $userList);
$router->get('user.detail', '/users/{id:\d+}', $userDetail);
$router->post('user.create', '/users', $userCreate);

assert($router instanceof Psr\Http\Server\MiddlewareInterface);

This is the preferred usage of the router, as it ensures that the request is properly set up for the route handler. Generally the router should be the last middleware in the stack.

If you prefer to use the router without middleware, the router also implements RequestHandlerInterface and can be used directly:

/** @var ServerRequestInterface */
$request = /* create server request */;

/** @var ResponseInterface */
$response = $router->handle($request);

Route Handlers

All route handlers MUST implement the RequestHandlerInterface interface:

namespace Acme;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class UserListHandler implements RequestHandlerInterface
{
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        /** @var array */
        $users = /* load from database, etc */;

        return new Response(200, ['content-type' => 'application-json'], json_encode($users));
    }
}

If it is preferable to lazy load handlers, the lazy-middleware package can be used:

use Northwoods\Middleware\LazyHandlerFactory;

/** @var LazyHandlerFactory */
$lazyHandler = /* create the factory */;

$router->post('user.create', '/users', $lazyHandler->defer(CreateUserHandler::class));

Reverse Routing

Reverse routing enables generating URIs from routes:

$uri = $router->uri('user.detail', ['id' => 100]);

assert($uri === '/users/100');

API

Router::add($name, $route);

Add a fully constructed route.

Router::get($name, $pattern, $handler)

Add a route that works for HTTP GET requests.

Router::post($name, $pattern, $handler)

Add a route that works for HTTP POST requests.

Router::put($name, $pattern, $handler)

Add a route that works for HTTP PUT requests.

Router::patch($name, $pattern, $handler)

Add a route that works for HTTP PATCH requests.

Router::delete($name, $pattern, $handler)

Add a route that works for HTTP DELETE requests.

Router::head($name, $pattern, $handler)

Add a route that works for HTTP HEAD requests.

Router::options($name, $pattern, $handler)

Add a route that works for HTTP OPTIONS requests.

Router::process($request, $handler)

Dispatch routing as a middleware.

If no route is found, the $handler will be used to generate the response.

Router::handle($request)

Dispatch routing for a request.

If no route is found, a response with a HTTP 404 status will be returned.

If a route is found, but it does not allow the request method, a response with a HTTP 405 will be returned.

Credits

Borrows some ideas from zend-expressive-fastroute for handling reverse routing.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].