Eddy/Route is an implementation of nikic/FastRoute and Middlewares/FastRoute package.
Similarly to League/Route, this library aims to provide a friendlier API for FastRoute, as well as adding a few tweaks.
It is not as feature rich as League/Route, but intends to be simple and flexible.
Eddy/Route adds a few tweaks to FastRoute's default behaviour:
-
The default DataGenerator stores routes in an
ArrayObject
instance instead of a plain array.- The idea here is to allow creating a Dispatcher instance before routes have been added, since an
ArrayObject
is passed by reference, while a plain array is copied. - Note: Work In Progress: FastRoute passes an empty array to the Dispatcher if no variable routes are set, which breaks this feature for variable routes added after the Dispatcher is created.
- This feature may be dropped in favour of old behaviour.
- The idea here is to allow creating a Dispatcher instance before routes have been added, since an
-
Various convenience methods.
- The Router object provides a number of convenience methods that wrap FastRoute's own methods. These helpers are a little less verbose than FastRoute's method names, and may be preferred.
-
PSR-15 Compliant Router
- Eddy/Route implements psr/http-server-middleware and makes use of the Middlewares/FastRoute package to allow the router to be added to any PSR-15 compliant dispatcher.
- The Router object provides a
psr15()
method to directly access the Middlewares/FastRoute instance.
Below is a basic example of a Router implementation using Zend Diactoros, Zend Request Handler Runner, and the Middleman psr-15 dispatcher.
$r = new Eddy\Route\Router();
$r->get('/', function () {
return new Zend\Diactoros\Response\JsonResponse('hello world');
});
$r->get('/test', function () {
return new Zend\Diactoros\Response\JsonResponse('hello test');
});
$d = new mindplay\middleman\Dispatcher([$r, new Middlewares\RequestHandler()]);
(new Zend\HttpHandlerRunner\Emitter\SapiEmitter())->emit($d->dispatch(
Zend\Diactoros\ServerRequestFactory::fromGlobals()
));