2009-12-09 68 views
1

我目前有Zend设置在每个模块的view/scripts/layout.phtml文件(即:/application/modules/moduleName/scripts/layout.phtml)中查找布局脚本。 。这是通过在application.ini文件(resources.layout[] =)中设置layout []为空(空白)Zend布局 - 一个“智能”布局选择器

问题是许多模块可能共享相同的布局。我不想将相同的确切布局复制到使用它的每个模块中。我知道我可以通过设置一个特定的路径(如resources.layout.layoutpath = /layoutPath)来设置一切,以便使用一个布局脚本,并且所有内容都将使用/layoutpath/layout.phtml,并且我知道可以使用$this->_helper->layout->setLayout('foobaz');

设置单个页面(或整个控制器,在init中)

问题在于某些模块将具有不同的布局,而不是“标准”布局,而且我不想将其设置为按控制器或按操作基准。我想为整个模块设置它,在一个地方设置(或直接通过代码/ Zend自动计算出来)。理想情况下,它会设置它当前的状态,但如果一个模块没有自己的layout.phtml,它将使用默认模块的布局。

那么...我该怎么做呢?

回答

2

有几种解决方案,选择适合自己的策略

1延长动作控制器

class App_Controller_Action extends Zend_Controller_Action 
{ 

    public function init() 
    { 
     parent::init(); 

     $moduleName = $this->getRequest()->getModuleName(); 
     $layoutPath = APPLICATION_PATH . '/modules/' . $moduleName . '/layouts'; 
     if (is_dir($layoutPath)) { 
      $this->view->addScriptPath($layoutPath); 
     }  
    } 
} 

,然后做像往常一样IndexController extends App_Controller_Action ...
如果布局文件APPLICATION_PATH . '/modules/' . $moduleName . '/layouts'目录中存在 - 这将NE代替的默认布局

2你可以写前端控制器插件

将模组管理和默认:;
class App_Controller_Plugin_ModuleSwitcher extends Zend_Controller_Plugin_Abstract 
{ 
    protected $_view = null; 

    public function routeShutdown(Zend_Controller_Request_Abstract $request) 
    { 
     $moduleName = $request->getModuleName(); 

     Zend_Layout::startMvc(); 
     $layout = Zend_Layout::getMvcInstance(); 
     $layout->setLayoutPath(APPLICATION_PATH . '/modules/' . $moduleName . '/layouts')->setLayout($moduleName); 

     return $request; 
    } 
} 

不要忘记谷歌另一个解决方案)

0

最快的解决方案可能是创建一个符号链接,将模块布局文件指向默认布局。这在Windows上不起作用,难以维护。

更好的是,在Bootstrap中创建一个方法来设置布局。

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 

    public function _initLayoutScript(){ 
     //ensure layout is setup 
     $this->bootstrap(array('layout', 'FrontController')); 

     $layout= $this->getResource('layout'); 

     $front = $this->getResource('FrontController'); 

     //do something with $layout and $front - set layout script/path etc based on request 
     //You could use file_exists to detect module layout scripts 

    } 

} 

详情请参阅http://framework.zend.com/manual/en/zend.application.quick-start.html#zend.application.quick-start.resources

最后,您可以使用write your own application resource与Zend_Application一起使用。

1

你可以在几步

1步中设置自己的布局选择。

步骤2: 每个模块管理/布局/脚本 和默认/布局/脚本 投入layout.phtml在创建布局文件夹

步骤3: 删除应用的layout.phtml文件/布局/脚本。

第4步: 使库内的Plugin文件夹和make Plugin。PHP 作为

class Plugin_Layout extends Zend_Controller_Plugin_Abstract 
{ 

    public function preDispatch(Zend_Controller_Request_Abstract $request) 

    { 
     $layoutPath = APPLICATION_PATH . '/modules/' . $request->getModuleName() . '/layouts/scripts/'; 
     Zend_Layout::getMvcInstance()->setLayoutPath($layoutPath); 
    } 
} 

步骤5:

开申请/ CONFIGS/Appication.ini文件 和编辑 作为

;resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/" 
resources.layout.layout = "layout" 
;register your plugin 

autoloaderNamespaces[] = "Plugin" 
resources.frontController.plugins[] = "Plugin_Layout" 

步骤6:

打开引导文件的应用/ Bootstrap 将代码放入

protected function _initAutoload() 

{ 

     $loader = new Zend_Application_Module_Autoloader(array(
        'namespace' => '', 
        'basePath' => APPLICATION_PATH . '/modules/' 
       )); 

     return $loader; 
    } 

    protected function _initPlugins() 

{ 

     $this->bootstrap('frontcontroller'); 
     $fc = $this->getResource('frontcontroller'); 
     $fc->registerPlugin(new Plugin_Layout()); 
}