2011-09-01 20 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')); 
} 

这样的检查,你需要做的数据库级别的处理,是属于和重定向/错误在控制器中处理,是属于它的。在请求周期中,视图太迟以至于检查诸如这样的业务逻辑“用户是否真的被允许看到这个?”,视图的作业只是输出信息。