ZF2 Controller mapping issue

89 Views Asked by At

I am new to Zend. I have set up local dev at zend.local I am creating a new module say Csv, when I go to URL like zend.local/csv, it gives me the following error enter image description here

My module.config.php is below:

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Csv\Controller\Csv' => 'Csv\Controller\IndexController',
        ),
    ),
     'router' => array(
         'routes' => array(
             'csv' => array(
                 'type'    => 'segment',
                 'options' => array(
                     'route'    => '/csv[/][:action][/:id]',
                     'constraints' => array(
                         'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                         'id'     => '[0-9]+',
                     ),
                     'defaults' => array(
                         'controller' => 'Csv\Controller\IndexController',
                         'action'     => 'index',
                     ),
                 ),
             ),
         ),
     ),

    'view_manager' => array(
        'template_path_stack' => array(
            'csv' => __DIR__ . '/../view',
        ),
    ),
);
2

There are 2 best solutions below

0
On

you have provided the wrong controller name :

             'defaults' => array(
                 'controller' => 'Csv\Controller\IndexController',
                 'action'     => 'index',
             ),

it should be

             'defaults' => array(
                 'controller' => 'Csv\Controller\Csv',
                 'action'     => 'index',
             ),

according to your config

'controllers' => array(
    'invokables' => array(
        'Csv\Controller\Csv' => 'Csv\Controller\IndexController',
    ),
),
1
On

You require the may_terminate option to ensure the router can regard csv as a route that it can terminate at.

'csv' => array(
    'type'    => 'segment',
    'options' => array(
        'route'    => '/csv[/][:action][/:id]',
        'constraints' => array(
            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id'     => '[0-9]+',
        ),
        'defaults' => array(
            'controller' => 'Csv\Controller\IndexController',
            'action'     => 'index',
        ),
    ),
    'may_terminate' => true // added
),