为模块库Zend应用程序选择不同的视图

时间:2011-03-11 22:59:49

标签: zend-framework

我已使用addModuleDirectory在我的应用程序中定义了模块:

  Application
                |
                modules
                           |
                           module1
                           |
                           module2

现在我想为我的Bootstrap.php中的每个模块设置不同的视图如何实现?

1 个答案:

答案 0 :(得分:3)

如果您想要的是根据模块切换布局而不是视图的方法,那么您需要的是标准front controller plugin

一个非常简单的实现是:

class LayoutModuleSwitcher extends \Zend_Controller_Plugin_Abstract
{
    /**
     * @param Zend_Controller_Request_Abstract Request object
     */
    public function routeShutdown(\Zend_Controller_Request_Abstract $request)
    {
        \Zend_Layout::getMvcInstance()
            ->setLayoutPath(APPLICATION_PATH . '/layouts/' . $request->getModuleName() . '/scripts/');
    }
}

具有以下布局结构:

application/layouts/module1/scripts/layout.phtml
application/layouts/module2/scripts/layout.phtml

取决于以下配置:

resources.frontController.plugins.LayoutModuleSwitcher = "Ahp\Controller\Plugin\LayoutModuleSwitcher"
resources.layout.layout = "layout"; The name of your layout.phtml without the suffix, needed to init layout