ds / router
Router Decorator with Cached FastRoute Controller Handler
Requires
- jeremeamia/superclosure: 2.3.*
- nikic/fast-route: 1.2.*
- psr/http-message: 1.0.*
- symfony/yaml: v3.2.*
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: 5.1.6
- symfony/routing: v3.2.3
Suggests
- nikic/fast-route: 1.2.*
- symfony/routing: v3.2.3
This package is not auto-updated.
Last update: 2018-01-20 10:32:59 UTC
README
Rs\Router
PHP Router
Adaptor Based Routing Component.
Includes the following Adaptors:
- Fast Route (with cached routes for String|Closure)
- Symfony Routing
Creating the Router
$router = new Rs\Router\Router(
Ds\Router\Interfaces\AdaptorInterface,
Ds\Router\Interfaces\RouteCollectionInterface
)
//Fast Router with Serialized Routes.
$router = new Router(
new FastRouteAdaptor(
new SuperClosure(
new Serializer()
),
(array) $options
),
new RouteCollection()
);
//Use the Factory methods for easy access
$router = \Ds\Router\RouterFactory::createFastRouter($options);
Router Adaptors
Adaptors must implement 'Ds\Router\Interfaces\AdaptorInterface
Provided Adaptors:
- FastRoute - Ds\Adaptors\FastRouteAdaptor
- Symfony - Ds\Adaptors\SymfonyAdaptor
Ds\Router\Adaptor\FastRouteAdaptor(
SerializerInterface $serializer,
array $options = []
);
$fastRoute = new FastRouteAdaptor(
new SuperClosure(
new Serializer()
),
[
'cacheDisabled' => FALSE,
'cacheFile' => __DIR__ . '/routes.cache',
'cacheExpires' => 60 * 60 * 24 * 7,
'errorHandlers' => [
'default' => [
'handler' => '\Site\Controllers\Error\ErrorController::error404',
'name' => ['error']
]
]
]
)
$router = $router->withAdaptor($fastRoute)
Route Handlers
//PSR Based.
function (
RequestInterface $request,
ResponseInterface $response,
callable $next = null
) {
if ($next){
$response = $next($request, $response);
}
return $response;
}
Route Example
$router->addRoute(
'get',
'/simple',
function ($request, $response){
return $response;
},
['routeName']
);
Using class dispatcher
//Load Dispatcher to convert handler intro response.
$dispatcher = new \Ds\Router\Dispatcher\Dispatcher([
'whitelist' => ['ControllerInterface'],
'blacklist' => ['ControllerInterface2'],
'autoWire' => true
]);
//$ontainer some DI-Container.
//Dispatch the request with params to pass to controller.
$dispatched = $dispatcher->dispatch(
$serverRequest,
$response->getHandler(),
['container' => $container]
);
var_dump($dispatched->getBody()->getContents());
Route Collections
Routes Collections must implement \Ds\Router\Interfaces\RouteCollectionInterface.
$routeCollection = new \Ds\Router\RouteCollection();
$routeCollection->group('/sub-dir', function() use ($routeCollection){
$routeCollection->addRoute(
(string)$httpMethod,
(string)$path,
(string)$handler,
(array)$names
);
});
$router = $router->withCollection($routeCollection)
Loaders
Collection Loaders must implement \Ds\Router\Interfaces\LoaderInterface.
//Or routes can be added via seperate files via the FileLoader
//Variables can be made accessible via 'vars' option.
$fileLoader = new Ds\Router\Loaders\FileLoader($router, [
'vars' => [
'varName' => $varName,
]
]);
//load routes from Routes/routes.php and Routes/routes2.php
$router = $fileLoader->loadFiles([
__DIR__ . '/path/to/file.php',
]);
####Routes Example
Using the router.
Once the adaptor and routes have been added Routes can be matched to the PSR7 request and a RS\Router\RouterResponse returned
Dispatching Routes.
Use Router::getRouterResponse() to return the RouterResponse for that request.
Dispatching Handlers.
Handlers must be resolved from the in order to get the response from the controller/closure. This is useful when dealing with expensive calls or to areas of the site than might not be accessible before/after middleware e.g. Authentication. Trying RouterResponse::getResponse() without resolving will cause an Exception to be thrown.