【问题标题】:CakePHP question: What's the difference between these two routing?CakePHP 问题:这两种路由有什么区别?
【发布时间】:2011-09-20 02:28:59
【问题描述】:

这是一个 =>

echo $this->Html->link('Edit',
                            array('controller'=>'comments','action'=>'edit',$comment['Comment']['id']));

这是另一种形式 =>

echo $this->Form->create('Comment', 
                array('url'=>array('controller' => 'comments', 'action' =>'add', $listposts['Post']['id']) )
                );
    echo $this->Form->input('post_id',array('type'=>'hidden','style'=>'width:30%','value'=>$listposts['Post']['id']));  
    echo $this->Form->input('name',array('style'=>'width:30%'));
    echo $this->Form->input('email',array('style'=>'width:30%'));   
    echo $this->Form->input('body',array('rows'=>'5'));

    echo $this->Form->end('Submit');

是否可以像上一个一样 echo $this->Form->create ?为什么我需要'url'=>array(..) 为什么不喜欢这个=>

echo $this->Form->create('Comment',array('controller' => 'comments', 'action' =>'add', $listposts['Post']['id']));

【问题讨论】:

    标签: php cakephp routing cakephp-1.3


    【解决方案1】:

    原因是 url-array 是 HTML-helper 的 link()-function 中的一个参数。如果你看一下声明:

    link(string $title, mixed $url = null, array $options = array(),  
         string $confirmMessage = false)
    

    因此,您将 url 作为第二个参数传递而没有 $url 名称,就像使用其他方法一样。

    但是,Form-helper 中 create() 方法的声明是:

    create(string $model = null, array $options = array())
    

    请注意,只有一个 $options-array。因此,您的 url 是数组的成员,而不是参数列表的成员。在关联数组中,您不能简单地保留键,因此使用 url-array 而不明确命名键不应该工作。

    有关更多信息,请参阅此方法的文档:
    HTML->link()
    Form->create()

    【讨论】:

    • 我怎样才能通过 $this->Html->link('Edit comment',array(..)); 将帖子 ID 作为第二个参数传递
    • 通过将其附加到 url 数组:echo $this->Html->link('Edit', array('controller' => 'comments', 'action' => 'edit', $comment['Comment']['id'], $post_id));。当然,这也会改变生成的 url:/comments/edit/comment_id/post/id.
    猜你喜欢
    • 2018-05-18
    • 1970-01-01
    • 2013-09-08
    • 2019-12-11
    • 2019-03-31
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多