在cakepp中,我有一个控制器,它应该接收一个参数,然后调用模型来处理数据库,以在视图中显示结果。相当常见的mvc方法。
将控制器想象为"插入一个新帖子",该帖子应该与特定用户关联。
因此,URL应该是:http://mysite/inspost/(user_id).
问题是,当URL类似于http://mysite/inspost/
时
即使没有指定user_id,它也会显示相同的视图并插入新的帖子。
我该如何控制?
从博客教程的2nd页面,添加一层:
public function view($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}