CakePHP从视图中指向其他url


CakePHP redict to other url from view

我想从视图重定向到其他视图。ctp。

假设如下=>

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

您在控制器中进行重定向,而不是在视图中。时期。
你可以在控制器中轻松地完成视图中所做的检查

我不知道你的具体情况,但我通常使用这种模式:

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'));
}

这样,你需要做的检查是在数据库级别上处理的,如果它属于,重定向/错误是在控制器中处理的,如果它属于。视图在请求周期中太迟了,无法检查业务逻辑,如"是否允许用户看到这个?",视图的任务只是输出信息