1

我使用Zend Framework構建CMS,我的主題可以支持自定義佈局,視圖和控制器。在Zend Framework中使用多個控制器目錄

如果主題具有自定義佈局,我將佈局目錄設置爲主題目錄。如果主題使用自定義視圖,我將視圖目錄設置爲主題目錄。

但我被contollers卡住因爲我想用我的默認控制器作爲後備。一個主題將只有它的自定義控制器不是每個控制器。例如,如果它支持Image Gallery功能,則必須有ImageGalleryController.php文件,但不包含IndexController或ErrorController控制器。

總結:主題可以覆蓋控制器,如果控制器存在於主題文件夾中使用它,否則使用默認值。

我該怎麼做?謝謝。

+0

你看過使用路線嗎? –

+0

我不認爲這與路線有關。 – cnkt

回答

0

我不確定我完全理解,但我認爲你可能要找的東西是模塊? 例如

Application/ 
    Configs/ 
    Modules/ 
     default/ 
      controllers/ 
      layouts/ 
      other standard folders (like views, models etc) 
     othermodule/ 
      controllers/ 
      layouts/ 
      other standard folders (like views, models etc) 
     etc/ 
      controllers/ 
      layouts/ 
      other standard folders (like views, models etc) 

然後,您只需創建一個漂亮的插件,並切換佈局。就像這樣:

<?php 
/** 
* This class will change the layout per modular basis. If a layout is not found, it will use the specified modular layout. 
* 
* @author Elliott Websites 
*/ 
class App_Modular_layout extends Zend_Controller_Plugin_Abstract 
{ 

    public function preDispatch(Zend_Controller_Request_Abstract $request) 
    { 
     $module = $request->getModuleName(); 
     $layout = Zend_Layout::getMvcInstance(); 

     $defaultLayout = APPLICATION_PATH . '/modules/default/layouts'; 
     $defaultLayoutName = 'default'; 

     if(file_exists(APPLICATION_PATH . '/modules/' . $module . '/layouts/' . $module . '.phtml')) 
     { 
      $layout->setLayoutPath(APPLICATION_PATH . '/modules/' . $module . '/layouts') 
          -> setLayout($module); 
     } else { 
      $layout->setLayoutPath($defaultLayout) 
          ->setLayout($defaultLayoutName); 
     } 
    } 

} 
+0

感謝您的努力,但正如我已經在我的問題中所說,我可以做到這一點。佈局和視圖沒有問題。我需要像你一樣的東西,但對於控制器。 – cnkt

+0

那麼這將違背整個MVC模型?認爲使用Zend組件構建自己的框架可能會更容易。 – elliottwebsites

相關問題