FastRoute Groups in Zend Expressive
I want to use route groups for FastRoute in Expressive. As a sample:
$router = $app->getContainer()->get(FastRoute\RouteCollector::class);
$router->get('/', App\Action\HomePageAction::class);
$router->addGroup('/pages', function (FastRoute\RouteCollector $router) {
$router->get('', App\Action\PagesIndexAction::class);
$router->get('/add', App\Action\PagesAddAction::class);
$router->get('/edit/{id}', App\Action\PageEditActionFactory::class);
$router->post('/edit/{id}', App\Action\PageEditActionFactory::class);
$router->get('/another/{section}[/{subsection}]', PagesAnotherActionFactory::class);
});
I created factories written in docs ( https://docs.zendframework.com/zend-expressive/features/router/fast-route/#advanced-configuration ) And register them in router.global.php
:
// ...
'factories' => [
FastRoute\RouteCollector::class => App\Container\FastRouteCollectorFactory::class,
FastRoute\DispatcherFactory::class => App\Container\FastRouteDispatcherFactory::class,
Zend\Expressive\Router\RouterInterface::class => App\Container\RouterFactory::class,
],
// ...
Now I cannot figure out where to write the config and how to activate it. Can this be done in a file config/router.php
? Help me please.
source to share
you can put them in config.router.php
while the file is merged with the rest of your config.
'dependencies' => [
//..
'invokables' => [
/* ... */
// Comment out or remove the following line:
// Zend\Expressive\Router\RouterInterface::class => Zend\Expressive\Router\FastRouteRouter::class,
/* ... */
],
'factories' => [
/* ... */
// Add this line; the specified factory now creates the router instance:
FastRoute\RouteCollector::class => App\Container\FastRouteCollectorFactory::class,
FastRoute\DispatcherFactory::class => App\Container\FastRouteDispatcherFactory::class,
// Zend\Expressive\Router\RouterInterface::class => Zend\Expressive\Router\FastRouteRouterFactory::class, // replaced by following line
Zend\Expressive\Router\RouterInterface::class => App\Container\RouterFactory::class,
/* ... */
],
],
Note the key dependencies
and your own RouterFactory
replaces FastRouteRouterFactory
, because it has the same config key.
source to share
This is not supported and I'm not sure if it can be implemented in FastRoute.
You can check Zend router flow - child routes
source to share