Set Different Layout for Different Module in Zend Framework 2?

自古美人都是妖i 提交于 2019-12-21 05:12:12

问题


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'));
}


来源:https://stackoverflow.com/questions/19568511/set-different-layout-for-different-module-in-zend-framework-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!