【问题标题】:CakePHP htmlhelper link method is omitting the id paramter of the request URLCakePHP html helper 链接方法省略了请求 URL 的 id 参数
【发布时间】:2012-06-14 14:47:56
【问题描述】:

我的视图包含一个用于更改页面语言的链接。索引,添加页面都很好。但是,当涉及到 URL 末尾有 ID 号的单篇文章的“编辑”或“查看”页面时,该数字在此链接上以某种方式被省略了。链接变成了

http://www.xxxxxx.com/index.php/categories/view/lang:chi

很明显,身份证号码不见了。链接应该是这样的

http://www.xxxxxx.com/index.php/categories/view/5/lang:chi

在视图文件中生成链接的代码是这样的

$this->Html->link($language, array('lang' => $code));

我不知道为什么只有最后一个 ID 号的 URL 有这样的问题。我使用了 bake 所以控制器视图操作是这样的:

    public function view($id = null) {
            $this->Category->id = $id;
            if (!$this->Category->exists()) {
                    throw new NotFoundException(__('Invalid category'));
            }
            $this->set('category', $this->Category->read(null, $id));
    }

有人知道怎么回事吗?

【问题讨论】:

    标签: cakephp html-helper


    【解决方案1】:

    如果你没有在链接数组中指定id,它就不会被使用。如果省略它们,则添加的唯一“魔法”是控制器和动作。

    创建链接时需要传递 id:

    $this->Html->link($language, array('lang' => $code, $id));
    

    【讨论】:

    • 谢谢。这行得通。虽然我必须检查我使用 $this->request-pass[0] 的 ID 是否存在,然后将其用作 $id(如果存在)。还有其他更简单的方法吗?
    • 你可以在控制器中设置它,因为它已经通过了,比如$this->set('id', $id);
    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多