2017 © Pedro Peláez
 

middleware routing-middleware

PSR 7 routing middleware

image

jowy/routing-middleware

PSR 7 routing middleware

  • Thursday, June 25, 2015
  • by Atriedes
  • Repository
  • 1 Watchers
  • 1 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Routing Middleware

Build Status, (*1)

PSR 7 routing middleware based on nikic/fast-route, (*2)

Installation & Requirements

Install using composer, (*3)

$ composer require jowy/routing-middleware

This library has following dependencies:, (*4)

  • 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, (*5)

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

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

$app->pipe($route_middleware);

Usage on relay/relay, (*6)

It advised to use container to resolve middleware when using relay/relay, (*7)

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., (*8)

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

    [
        "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, (*10)

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

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

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

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

    [
        "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, (*15)

    [
        "cacheDriver" => new ArrayCache()
    ]
    

License

MIT, see LICENSE, (*16)

The Versions

25/06 2015
25/06 2015