gpt4 book ai didi

php - Zend Expressive 中的 FastRoute 组

转载 作者:搜寻专家 更新时间:2023-10-31 21:22:20 25 4
gpt4 key购买 nike

我想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com