php CakePHP 将表单提交给正确的操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5285471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:52:02  来源:igfitidea点击:

CakePHP submitting a form to right action

phpcakephp

提问by benhowdle89

I have this in add.ctp:

我在 add.ctp 中有这个:

<!-- File: /app/views/posts/add.ctp --> 

<h1>Add Post</h1>
<?php
echo $form->create('Post');
echo $form->input('title');
echo $form->input('body', array('rows' => '3'));
echo $form->end('Save Post');
?>

and this in my controller:

这在我的控制器中:

function add(){
    if (!empty($this->data)) {
        if($this->Post->save($this->data)){
            $this->Session->setFlash('Your post has been saved');
            $this->redirect(array('action' => 'index'));
        }
    }
}

My question is how does CakePHP know that when the user hits submit, to send "data" to the function "add" in the controller?

我的问题是 CakePHP 如何知道当用户点击提交时,将“数据”发送到控制器中的“添加”函数?

回答by Andrea

By default CakePHP will send the form to the same action that displayed it.

默认情况下,CakePHP 会将表单发送到显示它的相同操作。

You can change it in the view as follows:

您可以在视图中进行如下更改:

echo $form->create('Post', array('action' => 'whatever'));

回答by dav

or if you want to redirect to another controller as well you can use this

或者如果你也想重定向到另一个控制器,你可以使用它

echo $form->create('Post', array('url' => '/controller_name/action_name'));

echo $form->create('Post', array('url' => '/controller_name/action_name'));

回答by Aditya P Bhatt

As per the updated syntax below will work (CakePHP 2.4.x):

根据下面的更新语法将起作用(CakePHP 2.4.x)

echo $this->Form->create('RegistrationsInout', array('action' => 'startroom'));

回答by Alimon Karim

For cakephp 3.x

对于cakephp 3.x

$this->Form->create('Post', ['url' => ['action' => 'post']]);

See doc

文档