jowy / routing-middleware
PSR 7 routing middleware
Requires
- php: >=5.5.0
- doctrine/cache: ^1.4
- jowy/exception-handling-middleware: ^1.0
- nikic/fast-route: ^0.6
- zendframework/zend-diactoros: ^1.1
- zendframework/zend-stratigility: ^1.0
Requires (Dev)
- henrikbjorn/phpspec-code-coverage: ^1.0@dev
- phpspec/phpspec: ^2.2@dev
This package is not auto-updated.
Last update: 2024-12-21 19:31:45 UTC
README
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 implementationzendframework/zend-stratigility
, provide abstraction for PSR 7 middlewarenikic/fast-route
, used for routingdoctrine/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 ofDoctrine\Common\Cache\Cache
[ "cacheDriver" => new ArrayCache() ]
License
MIT, see LICENSE