hidden_matthew
hidden_matthew

Reputation: 1

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);
        }
    ],
],

Upvotes: 0

Views: 1089

Answers (1)

unclexo
unclexo

Reputation: 3941

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,
    ],
],

Upvotes: 1

Related Questions