Zend Framework 2 Modules

I'm new to Zend Framework 2. I started using it a few days ago. For the last three days I have been struggling with the structure of the module. I would like to have 2 modules: main and administrative.

I have an application.config.php file:

return array(
    'modules' => array(
        'main', 'Administrator',
    ),
    'module_listener_options' => array(
        'module_paths' => array(
            './module',
            './vendor',
        ),
        'config_glob_paths' => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
    ),
);

      

module.config.php of the "administrator" module:

return array(
    'router' => array(
        'routes' => array(
            'Administrator' => array(
                'type' => 'Literal',
                'options' => array(
                    'route' => '/administrator',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Administrator\Controller',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Administrator\Controller\Index' => 'Administrator\Controller\IndexController',
            'Administrator\Controller\Blogs' => 'Administrator\Controller\BlogsController',
            'Administrator\Controller\Design' => 'Administrator\Controller\DesignController',
            'Administrator\Controller\FAQ' => 'Administrator\Controller\FAQController',
            'Administrator\Controller\Interests' => 'Administrator\Controller\InterestsController',
            'Administrator\Controller\Main' => 'Administrator\Controller\MainController',
            'Administrator\Controller\Pages' => 'Administrator\Controller\PagesController',
            'Administrator\Controller\RSS' => 'Administrator\Controller\RSSController',
            'Administrator\Controller\Users' => 'Administrator\Controller\UsersController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions' => true,
        'doctype' => 'HTML5',
        'not_found_template' => 'error/404',
        'exception_template' => 'error/index',
        'template_map' => array(
            'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
            'administ/index/index' => __DIR__ . '/../view/administrator/index/index.phtml',
            'error/404' => __DIR__ . '/../view/error/404.phtml',
            'error/index' => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

      

and module.config.php of the "main" module:

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Main\Controller',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),
                ),
            ),
            'Main' => array(
                'type' => 'Literal',
                'options' => array(
                    'route' => '/main',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Main\Controller',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Main\Controller\Index' => 'Main\Controller\IndexController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions' => true,
        'doctype' => 'HTML5',
        'not_found_template' => 'error/404',
        'exception_template' => 'error/index',
        'template_map' => array(
            'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
            'main/index/index' => __DIR__ . '/../view/main/index/index.phtml',
            'error/404' => __DIR__ . '/../view/error/404.phtml',
            'error/index' => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

      

Here's my file structure: structure

I edited this post a bit . Now it seems the problem is now only with layouts. Whatever I open it shows the "admin" module layout and the correct page content (or error message, it depends on whether the controller / module / action exists). So the problem seems to be with layouts only.

PS When I first listed the admin module in application.config.php:

'modules' => array(
    'Administrator', 'Main'
),

      

It only displays the "main" module layout and vice versa - when "Main" is the first item in the array of modules - the admin pointer is displayed everywhere.

+3


source to share


1 answer


'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions' => true,
    'doctype' => 'HTML5',
    'not_found_template' => 'error/404',
    'exception_template' => 'error/index',
    'template_map' => array(

        // The following key will be overriden, and the last loaded module config
        // is the one used, just comment it (for both modules config) the default 
        // behavior will pick-up the default/conventional layout.

        //'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',

        'administ/index/index' => __DIR__ . '/../view/administrator/index/index.phtml',
        'error/404' => __DIR__ . '/../view/error/404.phtml',
        'error/index' => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),

      



+1


source







All Articles