How do I set up a new layout to a new module in ZE

2019-08-01 17:22发布

问题:

Can someone help me to set up a layout for the administration module and set up a layout for the application module ? . In the image below you can see my folder structure:

This is the content of my module.config.php from the administration module:

<?php 
 return array(
     'controllers' => array(
         'invokables' => array(
             'Administration\Controller\Admin' => 'Administration\Controller\AdminController',
         ),
     ),
     // The following section is new and should be added to your file
     'router' => array(
         'routes' => array(
             'album' => array(
                 'type'    => 'segment',
                 'options' => array(
                     'route'    => '/administration[/:action][/:id]',
                     'constraints' => array(
                         'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                         'id'     => '[0-9]+',
                     ),
                     'defaults' => array(
                         'controller' => 'Administration\Controller\Admin',
                         'action'     => 'index',
                     ),
                 ),
             ),
         ),
     ),
      'view_manager' => array(
        //'base_path' => 'http://www.attila-naghi.com/',
        '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/layout2.phtml',
          //  'administration/admin/index'     => __DIR__ . '/../view/administration/admin/index.phtml',
            // 'error/404'                      => __DIR__ . '/../view/error/404.phtml',
            // 'error/index'                    => __DIR__ . '/../view/error/index.phtml',

), 'template_path_stack' => array( DIR . '/../view', ), ), ) ?> here i set up the layout. But for some reason if i access the application module, it loads the layout from the administration module. WHY ? this is the content of the module.config.php file from the application module:

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '[:controller[/:action]][/:param1]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
                            ),
                            'defaults' => array(
                                'action' => 'index',
                                '__NAMESPACE__' => 'Application\Controller',
                               // 'param1' => 'tralala'
                            )
                        )
                    )
                )
            ),
        ),
    ),

    'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
            'Zend\Log\LoggerAbstractServiceFactory',
        ),
        'aliases' => array(
            'translator' => 'MvcTranslator',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index'          => 'Application\Controller\IndexController',
            'Application\Controller\Create'         => 'Application\Controller\CreateController',
            'Application\Controller\Blog'           => 'Application\Controller\BlogController',
            'Application\Controller\Portofolio'     => 'Application\Controller\PortofolioController',
            'Application\Controller\User'           => 'Application\Controller\UserController',
        ),
    ),
    'view_manager' => array(
        'base_path' => 'http://www.attila-naghi.com/',
        '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',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
    // Placeholder for console routes
    'console' => array(
        'router' => array(
            'routes' => array(
            ),
        ),
    ),
);

and this is the application.config.file content:

 return array(
    // This should be an array of module namespaces used in the application.
    'modules' => array(
        'Application',
        'Administration'
    ), 
......

回答1:

I have recently found a way to do it (someone else's solution). Add this to your module.config.php (in my case the module is called Album, this is based on the demo application of ZF2):

'module_layouts' => array(
    'Album' => 'layout/layout.phtml'
),

The other necessary change needs to be done in the main Module.php file (add this onBootstrap method there and edit as may be needed):

public function onBootstrap($e) {
    $e->getApplication()
            ->getEventManager()
            ->getSharedManager()
            ->attach('Zend\Mvc\Controller\AbstractController', 'dispatch', 
                    function($e) {
                        $controller = $e->getTarget();
                        $controllerClass = get_class($controller);
                        $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
                        $config = $e->getApplication()->getServiceManager()->get('config');
                        if (isset($config['module_layouts'][$moduleNamespace])) {
                            $controller->layout($config['module_layouts'][$moduleNamespace]);
                        }
                    }, 100);
}


回答2:

You have to change layout from the controller. just specify this code above the ViewModel

$this->layout('administration/admin/index');


回答3:

The configs of all modules are merged into a single config. The last loaded module will overwrite the layout from the first module. You can use the module below to set layout per module.

https://github.com/EvanDotPro/EdpModuleLayouts



回答4:

As requested here is my way for different layouts. This was not my idea but since i cannot find the source i will post the code here. If someone does know please add the URL in comment and i will include it in the answer. If you read the sources i gave you in the other question and the above answers i am sure will understand what is happening here.

Module.php

use Zend\ModuleManager\ModuleManager;

> > public function init(ModuleManager $moduleManager)
>     {
>         $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
>         $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
>             // This event will only be fired when an ActionController under the MyModule namespace is dispatched.
>           
>             $controller = $e->getTarget();
              $controller->layout('backofficeLayout');
>             
>         }, 100);
>       
>       
>       
>     }

module.config.php

'view_manager' => array(
 'display_not_found_reason' => true,
 'display_exceptions'       => true,

    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index', 
    'template_path_stack' => array(
        'backoffice' => __DIR__ . '/../view',
    ),
    'template_map' => array(

        'backofficeLayout'    => __DIR__ . '/../view/layout/myaccount-backoffice.phtml',))


回答5:

I have just noticed that is better to use custom name for each of the layout in your layout folder. Instead of using layout.phtml use layout-mymodulename.phtml. It worked better along with the previously highlighted points.