开发者

CakePHP submitting a form to right action

I have this in add.ctp:

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

<开发者_JAVA百科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?


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

You can change it in the view as follows:

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


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


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

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


For cakephp 3.x

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

See doc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜