zendframework / zend-expressive-router

Router subcomponent for Expressive
BSD 3-Clause "New" or "Revised" License
28 stars 13 forks source link

Allow varying `RouteMiddlewareFactory` behavior #76

Closed weierophinney closed 6 years ago

weierophinney commented 6 years ago

This patch adds a new optional constructor argument to the RouteMiddlewareFactory, a string $routerServiceName. By default, it uses Zend\Expressive\Router\RouterInterface for the value. Users can, however, specify an alternate service class. This allows users to vary the RouteMiddleware per module or path-segregated middleware pipeline:

// In configuration:
return [
    'dependencies' => [
        'factories' => [
            \Api\Router::class => FastRouteRouterFactory::class,
            \Api\RouteMiddleware::class => new RouteMiddlewareFactory(\Api\Router::class),
        ],
    ],
],

// In config/pipeline.php:
$app->pipe('/api', [
    \Api\RouteMiddleware::class,
    DispatchMiddleware::class,
]);

The factory implements __set_state(), allowing serialization via var_export() (the mechanism used by the default expressive configuration caching mechanism).