jowy/routing-middleware

PSR 7 routing middleware

v1.0.1 2015-06-25 08:00 UTC

This package is not auto-updated.

Last update: 2024-04-13 15:50:16 UTC


README

Build Status

PSR 7 routing middleware based on nikic/fast-route

Installation & Requirements

Install using composer

$ composer require jowy/routing-middleware

This library has following dependencies:

  • zendframework/zend-diactoros, used for PSR 7 implementation
  • zendframework/zend-stratigility, provide abstraction for PSR 7 middleware
  • nikic/fast-route, used for routing
  • doctrine/cache, used for caching routes

Usage

Usage on zendframework/zend-stratigility

use Zend\Stratigility\MiddlewarePipe;
use Jowy\Routing\Routing;

$app = new MiddlewarePipe();
$route_middleware = new Routing($options);

$app->pipe($route_middleware);

Usage on relay/relay

It advised to use container to resolve middleware when using relay/relay

use Pimple\Container;
use Relay\Relay;
use Jowy\Routing\Routing;

$container = new Container();

$container["middleware"] = [
    Routing::class => function() {
        return new Routing($options);
    }
];

$resolver = function ($class) use ($container) {
    return $container[$class];
}

new Relay(array_keys($container["middleware"], $resolver);

Options

All options is in array, with key => value format.

  • collection (callable or string) contains registered route from RouteCollector

    [
        "collection" => function (RouteCollector $collector) {
            $collector->addRoute("GET", "/", function (ServerRequestInterface $req, ResponseInterface $res) {
                return $res;
            });
            $collector->addRoute("GET", "/home", function (ServerRequestInterface $req, ResponseInterface $res) {
                return $res;
            });
        }
    ]

    optionally you use class instead of closure for handling matched request

    [
        "collection" => function (RouteCollector $collector) {
            $collector->addRoute("GET", "/", "Fully\\Qualified\\ClassName:yourMethod");
        }
    ]
  • generator (object) implementation of FastRoute\DataGenerator

    [
        "generator" => new FastRoute\DataGenerator\GroupCountBased();
    ]
  • parser (object) implementation of FastRoute\RouteParser

    [
        "parser" => new FastRoute\RouteParser\Std();
    ]
  • dispatcher (callable) callable that return implementation of FastRoute\Dispatcher

    [
        "dispatcher" => function ($dispatch_data) {
            return new FastRoute\Dispatcher\GroupCountBased($dispatch_data);
        }
    ]
  • cache (boolean) toggle routes caching, default value is false

    [
        "cache" => true
    ]
  • cacheDriver (object) if cache is enabled you have to pass this param to options. It must contain implementation of Doctrine\Common\Cache\Cache

    [
        "cacheDriver" => new ArrayCache()
    ]

License

MIT, see LICENSE