我想在表达中使用快速路线的路线组。
喜欢的样品:
$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);
});
我在文档中创建了工厂(https://docs.zendframework.com/zend-expressive/features/router/fast-route/#advanced-configuration)
并在
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,
],
// ...
现在我不知道在哪里编写配置以及如何激活它。
这可以在
config/router.php
文件中完成吗?请帮帮我。
最佳答案
只要文件与配置的其余部分合并,就可以将它们放入config.router.php
中。
'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,
/* ... */
],
],
注意
dependencies
键,并且您自己的RouterFactory
替换了FastRouteRouterFactory
,因为它共享相同的配置键。关于php - Zend Expressive中的FastRoute组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44094307/