Set Different Layout for Different Module in Zend

2019-03-31 05:02发布

问题:

I have two module Student and Teacher. I also have two different layout one is studentlayout.phtml and another is teacherlayout.phtml

How can I set studentlayout for Student module and teacherlayout for Teachermodule?

As Per Sam's answer .Thanks Its working fine.

but i also want to set two different layout For Teacher. So i add following code in my main config file for project:

'module_layouts' => array(

    'Teacher' => array(
      'default' => 'layout/adminlayout',
      'login'    => 'layout/loginlayout',
    ),
    'Student' => 'layout/studentlayout',
),

My module.config.php file for teacher module:

    'module_layouts' => array(

    'Teacher' => array(
      'default' => 'layout/adminlayout',
      'login'    => 'layout/loginlayout',
    ),
      'Student' => 'layout/studentlayout',
 ),

But all time all action of Teacher module take adminlayout. why login action can't take loginlayout?its ovveride?

回答1:

Usage

Using EdpModuleLayouts is very, very simple. In any module config or autoloaded config file simply specify the following:

array(
    'module_layouts' => array(
        'Teacher' => 'layout/teacher',
        'Student' => 'layout/student'
    ),
);

That's it! Of course you need to define those layouts, too... just check Application Modules module.config.php to see how to define a layout.



回答2:

If you only want to change layout for your one action you can use layout() plugin in your controllers action, or if you want different layout for all actions in one controller only in your module you can do it in bootstrap:

public function onBootstrap(\Zend\EventManager\EventInterface $e) {
    $eventManager = $e->getApplication()->getEventManager();
    $sharedEventManager = $eventManager->getSharedManager();
    $sharedEventManager->attach('Auth\Controller\AuthController', \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
}

public function onDispatch(MvcEvent $e) {
    $controller = $e->getTarget();      
    $controller->layout('layout/loginLayout');
}

After each action in that controller you will change root ViewModel layout you can go further and specify here more controllers where you want your layout like this

$sharedEventManager>attach(array('Auth\Controller\AuthController',
'Auth\Controller\Registration'),
\Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
}