作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在 Expressive 中为 FastRoute 使用路由组。喜欢 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);
});
我按照文档中的描述创建了工厂 ( 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/
我正在尝试使用 FastRoute路由库,无法使简单的用法示例起作用。 这是在 GitHub page 上找到的基本用法示例: addRoute('GET', '/users', 'get_all_u
我想在 Expressive 中为 FastRoute 使用路由组。喜欢 sample : $router = $app->getContainer()->get(FastRoute\RouteCol
我是一名优秀的程序员,十分优秀!