The requested controller could not be mapped to an existing controller class. zf3 with routes to several controllers in one module

When i'm trying to create routes to several controllers in one module and i'm getting this

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

Calling the indexAction() of IndexController works perfectly. However it doesn't work for VehiclesController. I tried to invoke VehiclesController in factories - unsuccessful.

'controllers' => [
    'factories' => [
        Controller\IndexController::class => InvokableFactory::class,
        Controller\VehiclesController::class => InvokableFactory::class,
    ],
    ],

Using ServiceManager function

'controllers' => [
    'factories' => [
        Controller\VehiclesController::class => function($sm){
            $vehiclesService=$sm->get('Application\Model\VehiclesTable');
            return new Controller\VehiclesController($vehiclesService);
        }
    ],
],

1 answer

  • answered 2017-11-13 07:46 unclexo

    May be you are doing something like below to invoke your controllers in your module.config.php

    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
            Controller\IndexController::class => InvokableFactory::class,
        ],
    ],
    

    Therefore, you may be using same controller name twice under factories key. It may be as above or different way.

    So define them once under factories subkey of controllers key as the following

    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
            Controller\VehiclesController::class => InvokableFactory::class,
        ],
    ],