首页 > 解决方案 > Zf3:每个模块中的类似Controllers

问题描述

我的 ZF2 应用程序中有不同的模块,每个模块都包含一个控制器列表。现在在 zf2 中创建路由和获取带有服务定位器的实体管理器是不同的。现在在 Zf3 中,我们需要添加“别名”和“工厂”以使用那里的任何资源,因为在为每个模块创建路由时,即使在不同的模块中也无法为控制器添加相同的别名;

这是我的应用程序/module.conf.php

 'controllers' => [
    'factories' => [
        Controller\IndexController::class => ServiceLocatorControllerFactory::class,
        Controller\UserController::class => ServiceLocatorControllerFactory::class,
    ],
    'aliases' =>  [
        'index'     =>  IndexController::class,
        'user'     =>  UserController::class,
    ]
],

和我的仪表板/module.config.php

 "controllers" => [
    'factories' => [
        Controller\UserController::class            => ServiceLocatorControllerFactory::class,
        Controller\WidgetController::class          => ServiceLocatorControllerFactory::class,
    ],
    'aliases' =>  [
        "user"          => UserController::class,
        "widget"            => WidgetController::class,
    ]
],

现在当我尝试访问/application/user/index

它转到仪表板 => UserController => IndexAction

而不是 Application => UserController => IndexAction

我现在的解决方案是为每个控制器手动创建路由,这在我的情况下真的很难,因为应用程序非常大并且有 100 个控制器。另外,写每条路线有点多余。有没有办法解决问题

标签: phpzend-frameworkzend-framework3

解决方案


由于没有为不同模块中的类似模块找到答案,我为每个模块中的每个控制器创建了单独的路由。这是我写的路线

应用程序-> 索引控制器

        'application-index' => [
            'type'  =>  Segment::class,
            'options' =>  [
                'route' =>  '/application/index[/][:action][/]',
                'defaults'  =>  [
                    '__NAMESPACE__' =>  'Application\Controller',
                    'controller'  =>  IndexController::class,
                    'action'      =>  'index'
                ],
            ]
        ],

我为我的应用程序中的每个控制器创建了。


推荐阅读