2010-11-25 81 views
1

我有一個控制器插件,檢測網站是否已設置爲維護,如果有,我希望它向前 - 維護控制器顯示'抱歉...'消息。zend _forward控制器插件

我不想使用重定向,因爲這會改變用戶所在的當前url,但_forward是一個Zend_Controller_Action受保護的方法,所以不能在上下文之外調用,我該怎麼做?

回答

6

當您的插件的preDispatch方法被調用,請求不會被調度。所以你可以通過設置控制器和請求動作來「轉發」:

public function preDispatch(Zend_Controller_Request_Abstract $request) 
{ 
    if ($this->isMaintenanceMode()) { 
     $request->setControllerName('error'); 
     $request->setActionName('maintenance'); 
    } 
} 
+0

我喜歡這種方式。我爲什麼沒有想到它? ;) – 2010-11-25 16:33:45

0

由於插件無法正確地_forward()在你的控制器(S)init()方法,你可以檢查,說你的網站處於維護模式的動作是不是你的維護行動標誌然後$this->_forward('maintenance-action');

喜歡的東西:

// .. in your action controller 
public function init() { 
    $maintenanceMode = checkTheFlagYourPluginSet(); 
    // .. other code removed omitted 
    if ($maintainenceMode && $this->_request->getActionName() != 'maintenance-action') { 
     // return so that nothing else gets executed in the init() 
     return $this->_forward('maintenance-action'); 
    } 
}