0

在 cakephp 中,我有一个控制器,它应该接收一个参数,然后调用模型来使用数据库以在视图中显示结果。很常见的 mvc 方法。

将控制器想象为应该与特定用户关联的“插入新帖子”。

所以,网址应该是:http://mysite/inspost/(user_id).

问题是,当 URL 像http://mysite/inspost/

即使未指定 user_id,它也会显示相同的视图并插入新帖子。

我该如何控制这个?

4

1 回答 1

2

从博客教程的第 2添加图层

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);
}
于 2014-04-14T14:40:12.790 回答