Vladomir
Vladomir

Reputation: 11

Layout per module zf2

I have 2 modules, and I need to use different layout for each one, but all modules always use layout of second loaded module.

application.config.php :

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

Upvotes: 0

Views: 795

Answers (3)

blackbishop
blackbishop

Reputation: 32650

You could use EdpModuleLayouts. It is a ZF2 module that allows you to configure different layout for each module. Its usage is very simple :

  • Just install it in your vendor folder of you project
  • Enable EdpModuleLayouts module in the application.config.php file :

    'modules' => array(
    'EdpModuleLayouts', //<---add this line
    'News',
    'Application',
    ),
    
  • Define the layout for each module in your module.config.php file of one of two modules, for example in the Application module. Here we are setting two layouts: news/layout for News Module and layout/layout for Application module :

    'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'XHTML1_TRANSITIONAL',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        'news/layout'    => __DIR__ . '/../../News/view/layout/admin-layout.phtml',
        '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(
        'application' => __DIR__ . '/../view',
        'news' => __DIR__ . '/../../News/view',
    ),
    ),
    
    'module_layouts' => array(
            'Application' => 'layout/layout',
            'News' => 'news/layout',
    ),
    

Upvotes: 2

Andres Echeverry
Andres Echeverry

Reputation: 89

I also had the same case and solve it like this:

Had the module login and application, what I did was to determine the layouts in global.php like this:

return array(
     'module_layouts' => array(
        'Application' => 'layout/layout.phtml',
        'Login' => 'layout/login.phtml' 
),
);

Now you should create a folder in the view layout login, where would the login.phtml

now in the configuration of login module. Module.config.php

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

This should run you.

Upvotes: 0

Edux87
Edux87

Reputation: 1

  1. Set in your {MyNewModule}/Module.php.

    namespace {MyNewModule};
    
    use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
    use Zend\Mvc\ModuleRouteListener;
    use Zend\ModuleManager\ModuleManager;
    use Zend\Mvc\MvcEvent;
    
  2. Create a new Method Init.

    public function init(ModuleManager $manager){
    
    $events = $manager->getEventManager();
    $sharedEvents = $events->getSharedManager();
    $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
        $controller = $e->getTarget();
        $controller->layout('layout/***customFile.phtm***');
    }, 100);
    
    }
    
  3. F5, recharge you browser.

  4. Smile :D

Upvotes: 0

Related Questions