Multiple layouts for different modules - Zend Framework

大城市里の小女人 提交于 2019-12-10 06:18:19

问题


I have a question about layouts in Zend Framework. This is my structure of my project:

  • I have 2 modules named "backoffice" and "frontoffice".
  • I have one layout.phtml in layouts/scripts for both the backoffice and frontoffice.
  • Now I want seperate "layouts/scripts" for "backoffice" and "frontoffice"
  • In my application.ini I have: resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

Now how can I fix this that I have seperate layouts?


回答1:


Just place another layout in the layout/scripts folder and tell any module, controller or action to use that other layout instead of the default layout.

If you want to let a controller use a different layout, you can place the following in your init()

$this->_helper->layout->setLayout('layoutname');

You can do that respectively for specific actions or for a whole module.




回答2:


Put this into your application.ini

resources.layout.layout = "layout"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

Your layout file will be /modules/MODULE_NAME/views/scripts/layout.phtml




回答3:


You should write front controller plugin for that purpose (called layout selector).

In your Bootstrap.php register that plugin - layout selector:

protected function _initPlugins(){
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new My_Plugins_LayoutSelector());
}

Auto load namespace My_ in application.ini

Autoloadernamespaces[] = "My_"

And finally, create in /library a new folder 'My' and in it folder 'Plugins' and in it file 'LayoutSelector.php' with code:

class My_Plugins_LayoutSelector extends Zend_Controller_Plugin_Abstract {
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $module = $request->getModuleName();
        $layout = Zend_Layout::getMvcInstance();
        $layout->setLayout($module);
    }
}

In this way every module in the future will use appropriate layout and no need to write in each controller to select layout.




回答4:


I use this custom plugin with some changes, and for each module I create specific layout, with specific structure http://blog.vandenbos.org/2009/07/19/zend-framework-module-specific-layout/



来源:https://stackoverflow.com/questions/14384100/multiple-layouts-for-different-modules-zend-framework

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