简体   繁体   English

当参数未通过url传递给控制器​​时

[英]When parameter is not passed to controller over url

In cakephp, I've a controller that should receive a parameter and then call model to work with database to show the result in a view. 在cakephp中,我有一个控制器,该控制器应接收一个参数,然后调用模型与数据库一起使用以在视图中显示结果。 Pretty common mvc approach. 相当常见的mvc方法。

Imagine the controller as "Insert a new post" which should be associated to an specific user. 将该控制器想象为“插入新帖子”,它应该与特定用户相关联。

So, the URL should be: http://mysite/inspost/(user_id). 因此,URL应为: http://mysite/inspost/(user_id).

The problem is, when URL is like http://mysite/inspost/ 问题是,URL如http://mysite/inspost/

It will show the same view and will insert the new post even if the user_id has not been specified. 即使未指定user_id,它也会显示相同的视图并插入新帖子。

How can I control this? 我该如何控制?

From the 2 nd page of the blog tutorial, Adding a layer : 在博客教程的第二页上, 添加一个层

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM