Module with multiple controllers in Zend 2

Asked

Viewed 190 times

2

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

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

How do I adjust the routes to get these Urls?

1 answer

1


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

Routing and controllers


1st Solution

For what you describe, the example in the documentation for a name module Album with several shares in a controller with the name AlbumController deal well with your problem:

Páginas do módulo "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

Multiples controllers is also possible if necessary and can be achieved by declaring the various controllers and the 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 you’ll have something like this:

view/
  album/
    album/             
      index.phtml
    user/             
      index.phtml

Browser other questions tagged

You are not signed in. Login or sign up in order to post.