Zend Framework 3 : How to route to specific controller by url

zend-mvc

#1

In my module’s module.config.php, I have something like this:

'controllers' => [
    'factories' => [
        Controller\MainappController::class => 
                Controller\Factory\MainappControllerFactory::class,
        //...
        Controller\VoucherController::class => 
                Controller\Factory\VoucherControllerFactory::class,
        //...
    ],
],    
'router' => [
    'routes' => [
        'mainapp' => [
            'type'    => Segment::class,
            'options' => [
                'route' => '/mainapp[/:action[/:first_id][/:second_id]]',
                'constraints' => [
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ],
                'defaults' => [
                    'controller' => Controller\MainappController::class,
                    'action'     => 'index',
                ],
            ],
        ],
        'voucher' => [
            'type'    => Segment::class,
            'options' => [
                'route' => '/voucher[/:action[/:first_id][/:second_id]]',
                'constraints' => [
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ],
                'defaults' => [
                    'controller' => Controller\VoucherController::class,
                    'action'     => 'index',
                ],


            ],
        ],
    ],
],

voucher is new Controller in same module

I can go to public/mainapp,

but when I try to public/voucher

I have an Error

A 404 error occurred Page not found.

The requested URL could not be matched by routing.

Please let me know , what’s wrong with this configuration.


#2

Where is /public defined in your routing?

Also, note that this can’t really work: '/mainapp[/:action[/:first_id][/:second_id]]' - the first_id and second_id match is ambiguous. They should probably be nested instead: '/mainapp[/:action[/:first_id[/:second_id]]]'