简体   繁体   中英

FastRoute Groups in Zend Expressive

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.

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.

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 "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM