2011-07-21 51 views
0

我想設置一個維護頁面,以便在禁用該站點時,無論請求哪個頁面,它都應該顯示。在CakePHP中重定向到維護頁面不起作用

目前,我試着用$this->cakeError()這樣做:

app_controller.php

function beforeFilter(){ 
.... 
if($this->__get_config('maintenance_status') == 1){ 
     $this->cakeError('maintenance', array('message' => $this->__get_config('maintenance_message')));  
    } 
.... 
} 

app_error.php

function maintenance($message){ 
    $this->controller->set('message', $message['message']); 
    ($this->controller->RequestHandler->isAjax()) ? $this->_outputMessage('ajax_maintenance') : $this->_outputMessage('maintenance'); 
} 

的問題是,出現致命錯誤,它說:Call to a member function isAjax() on a non-object 。但我明顯地設置了app_controller.php中的RequestHandler組件。此外,我試圖從另一個控制器中調用這個錯誤,它不會給我任何致命錯誤。

可能是什麼問題?爲什麼它不承認我已經初始化了組件?

+0

到底在哪在閱讀更多關於它'AppController'是被調用的錯誤? – cspray

+0

在'beforeFilter()'方法中。我編輯了我的帖子以反映這一變化。 – linkyndy

回答

0

From the CakePHP book

調用此方法會顯示一個錯誤頁面給用戶,並在您的應用程序停止任何進一步的處理

我假設你正在調用一些錯誤回撥在AppController

如果是這種情況,您很可能會在組件實例化之前停止執行腳本。這肯定會導致你的錯誤。

現在,我認爲這個錯誤是一個很好的機會來重新評估你如何處理這個問題。這真的是一個錯誤?您知道維護狀態設置爲預計,用戶將顯示此頁面。這不是一個錯誤。此外,您當然不希望日誌中的10,000條消息告訴您已關閉維護!

我認爲這可以通過使用一些控制器回調和一點點代碼更好地解決。

我不知道_get_config()是什麼,所以我認爲它是一個自定義用戶函數,您可以在此回調中調用。

我們將使用beforeFilter()控制器回調。現在

class AppController extends Controller { 

    public function beforeFilter() { 
     if ($this->_get_config('maintenance_status') === 1) { 
      $this->redirect('/maintenance'); 
     } 
    } 

} 

,你可以設置一個維護控制裝置,連接到其自己的觀點,這將正確顯示您的維修信息,並在錯誤日誌維護期間將無法登錄所有這些連接嘗試。

+0

感謝您的回覆。這是我之前嘗試過的方式,它不起作用,這就是爲什麼我第二次嘗試使用'cakeError()'。在我第一次嘗試創建'MaintenanceController'時,添加了一個'maintenance()'方法,並創建了它的視圖'maintenance.ctp'。然後,在'app_controller.php'中的'beforeFilter()'方法中,我添加了:if($ this - > __ get_config('maintenance_status')== 1){this-> redirect(array ('controller'=>'maintenance','action'=>'maintenance')); \t \t \t}'但是我的瀏覽器告訴我「頁面沒有正確重定向」,我不知道爲什麼。 – linkyndy

+0

爲什麼重定向不起作用,您的error_log中是否有任何錯誤?你確定你有'maintenance.ctp'在'views'裏面的'maintenance'目錄嗎? – cspray

+0

對不起,我應該在哪裏尋找那個error_log? :) – linkyndy

0

稍微好一點的也是使用Configure :: read(「System.maintenance」)或類似的東西。 (我傾向於命名空間我的配置數據,系統是維護標誌等東西的命名空間)

此外,正如查爾斯所說 - 不要使用預期事件的錯誤頁面。錯誤是向用戶顯示,併爲應用程序處理通知等,以防意外的故障。維護頁面可以簡單地作爲/ app/views/pages /文件夾中的視圖文件。重定向到如果配置鍵設置爲true/1。

0

你的方法似乎很聰明,但你可能會做得太過分了。

我在我正在開發的站點中有類似的設置,我只是使用auth組件來爲我照顧它。

爲了解決問題,我設置了一個新的離線佈局,強制應用程序在站點狀態爲0(脫機)時使用。如果狀態爲0,app_controller拒絕訪問整個站點。

$this->Auth->deny('*'); 
$this->layout = "offline"; 

而且,在這個佈局我有出現,如果用戶點擊message.If用戶能夠進行身份驗證(所有用戶現在 - 發展)一個隱藏的登錄表單訪問是使用授予整個網站默認模板。

檢查出來,它可以幫助你了...

Click Here

一些代碼,但你可以在上面的鏈接

function beforeFilter(){ 
// Site Offline = 0 , Site Online = 1 
if($this->Configuration->get_site_status() == 1){ 
     // Allow access to the site to all users and perform all required 
     // beforeFilter code 
}else{ 
    ... 
    // If site is OFFLINE but User is logged in allow access. 
    // Later I will need to change it to only allow admin access if logged in as I am still developing 
    // Everyone else will be denied access even if they are able to authenticate   
    if(!$this->Auth->user() == null){ 
     $this->layout = 'default'; 
     $this->Auth->allow('*'); 
    }else{   
     $this->layout = 'offline'; 
     $this->Auth->deny('*'); 
    } 
    ... 
    } 
} 
相關問題