特定于模块的布局插件用作.ini资源,但在引导程序中注册时则不工作



我目前正在为每个模块加载不同的布局文件。

我已将以下内容添加到我的配置.ini文件中

; Module Support
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
; Module-based Layout Support
resources.layout.pluginClass= "Layout_Plugin_ModuleLayout"

和以下控制器插件:

class Layout_Plugin_ModuleLayout extends Zend_Layout_Controller_Plugin_Layout {    
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $this->getLayout()->setLayoutPath(
            Zend_Controller_Front::getInstance()
                ->getModuleDirectory($request->getModuleName()) . '/layouts'
        );
        $this->getLayout()->setLayout('layout');
    }   
}

一切正常,但我更愿意将此插件与其他插件一起注册到引导程序中。当我将此插件移动到 Bootstrap 文件并像这样注册它时:

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

我收到以下错误:

致命错误:在第 31 行的 C:\workarea\web_projects\gam\trunk\website\library\Layout\Plugin\ModuleLayout.php 中的非对象上调用成员函数 setLayoutPath((

显然,我做错了什么或误解了这个插件的工作原理。

编辑:最终在 http://dustint.com/post/28/per-module-zend_layout 使用了解决方案的修改版本。但是,我愿意接受有关此的建议。这个解决方案使用普通的控制器插件,而我怀疑我应该让我们成为布局插件类型。但是,它奏效了。

问题是布局资源初始化了几件事,如果你查看Zend_Layout_Controller_Plugin_Layout的来源,你需要传递布局来使用,所以你可能需要在你的引导上这样做:

protected function _initLayouts()
{
     $this->bootstrap('layout');
     $this->bootstrap('frontController');
     $layout = $this->getResource('layout');
     $front = $this->getResource('frontController');
     $front->registerPlugin(new Layout_Plugin_ModuleLayout($layout));
}

最终在 http://dustint.com/post/28/per-module-zend_layout 使用了解决方案的修改版本。

最新更新