phpzend-frameworkzend-framework3

Zf3: similar Controllers in each module


I have different modules in my ZF2 application and each module contains a list of Controllers. Now when in zf2 it was different to create routes and get Entity Manager with service locator. Now in Zf3 we need to add "aliases" and "factories" to use any resources there for when creating routes for each module it is not possible to add same alias for controller even when in different modules;

here is my application/ module.conf.php

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

and my dashboard / module.config.php

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

now when i try to access /application/user/index

it goes to Dashboard => UserController => IndexAction

instead of Application => UserController => IndexAction

The solution that i have now is to create manually routes for each controller, which is really hard in my case as the application is really big and there are 100's of controller. plus it is kinda redundent task to write every route. Is there anyway to solve the issue


Solution

  • As no answer was found for similar modules in different module, i created seperate routes for each controller in each module. This was the route i wrote for

    Application-> IndexController

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

    and i created for each and every controller that i have in my application.