Module with multiple controllers in Zend 2

2

I'm having trouble creating a module with several controllers in Zend 2. It would be, for example:

admin/user/
admin/user/add
admin/user/edit
admin/user/delete

How do I adjust the routes for these URLs?

    
asked by anonymous 03.04.2015 / 03:20

1 answer

1

In the file module.config.php of your module, you have to declare invokables to controllers as well as add routes to every controller .

Routing and controllers


1st Solution

For what you describe, the example in the documentation for a module named Album with multiple actions in controller with name AlbumController handles well with your problem:

Pagesofthemodule"Album"

Contents of the file module.config.php :

return array(
     'controllers' => array(
         'invokables' => array(
             'Album\Controller\Album' => 'Album\Controller\AlbumController',
         ),
     ),

     // The following section is new and should be added to your file
     'router' => array(
         'routes' => array(
             'album' => array(
                 'type'    => 'segment',
                 'options' => array(
                     'route'    => '/album[/:action][/:id]',
                     'constraints' => array(
                         'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                         'id'     => '[0-9]+',
                     ),
                     'defaults' => array(
                         'controller' => 'Album\Controller\Album',
                         'action'     => 'index',
                     ),
                 ),
             ),
         ),
     ),

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


2nd Solution

Multiple controllers is also possible, if necessary and can be achieved by declaring the various controllers and various routes as follows:

'controllers' => array(
    'invokables' => array(
        'Album\Controller\Album' => 'Album\Controller\AlbumController', // 1º controller
        'Album\Controller\User' => 'Album\Controller\UserController',   // 2º controller
    ),
),

And the routes:

'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),
        'user' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/user[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\User',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

Where will you get something like this:

view/
  album/
    album/             
      index.phtml
    user/             
      index.phtml
    
03.04.2015 / 14:19