2011-09-01 50 views
0

我想重定向到view.ctp的其他視圖。CakePHP redict從其他網址查看

假設這樣=>

if($val == 0) { 
    redirec to 'posts/index' 
} 

我怎麼能這樣做呢?

+3

你正在殺MVC! – pleasedontbelong

+1

http://book.cakephp.org/view/982/redirect,但在控制器中執行。 – JJJ

+0

是什麼原因從視圖中重定向? – deceze

回答

1

您在控制器中進行重定向,而不是在視圖中。期。
您可以輕鬆地在控制器視圖中執行相同的檢查。

我不知道你的具體情況,但我通常使用這個模式:

public function view($id) { 
    $post = $this->Post->find('first', array(
     'conditions' => array('Post.id' => $id, 'Post.mark' => 1) 
    )); 
    if (!$post) { 
     $this->cakeError('error404'); 
     // or redirect, or show a more specific error page, or do something else 
    } 

    $this->set(compact('post')); 
} 

這樣的檢查,你需要做的數據庫級別的處理,是屬於和重定向/錯誤在控制器中處理,是屬於它的。在請求週期中,視圖太遲以至於檢查諸如這樣的業務邏輯「用戶是否真的被允許看到這個?」,視圖的作業只是輸出信息。