Reputation: 41
I want use route groups for FastRoute in Expressive. Like 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 as written in docs (https://docs.zendframework.com/zend-expressive/features/router/fast-route/#advanced-configuration)
And register their 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 can not figure out where to write the configuration and how to activate it.
Can this be done in the file config/router.php
?
Help me, please.
Upvotes: 4
Views: 931
Reputation: 4898
This is not supported and I am not sure if this can be implemented in FastRoute.
You can check the thread "Zend router - child routes"
Upvotes: 0
Reputation: 736
you can put them in config.router.php
as long as the file gets 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 dependencies
key and that your own RouterFactory
replaces the FastRouteRouterFactory
because it shares the same config key.
Upvotes: 1