CakePHP的3.6.14:控制器获得POST请求,重新打开查看和复制表单元素

问题描述 投票:0回答:1

我想,当用户提交表单,然后这个表单的内容将被保存在数据库中,但随后会重定向到相同的形式相同,前一个,使用户可以编辑其中的一些,重新保存等领域。我怎样才能做到这一点?

add.ctp:

<div class="roles form large-9 medium-8 columns content">
<?= $this->Form->create($role) ?>
<fieldset>
    <legend><?= __('Add Role') ?></legend>
    <?php
        echo $this->Form->control('name');
        echo $this->Form->control('description');
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

RolesController.php(添加功能)

public function add()
{
    $role = $this->Roles->newEntity();
    if ($this->request->is('post')) {
        $role = $this->Roles->patchEntity($role, $this->request->getData());
        if ($this->Roles->save($role)) {
            $this->Flash->success(__('The role has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The role could not be saved. Please, try again.'));
    }
    $this->set(compact('role'));
}
php post cakephp request cakephp-3.x
1个回答
2
投票

对于住在同一页/表单只是删除return $this->redirect(['action' => 'index']) - 但我不认为这是你真正想要的。用户将死结束了“添加”页面上 - 只需添加更多的实体...

退房的CakePHP Controller tutorial - 它展示了如何创建edit方法和模板。这样做之后,你必须重定向更改为类似return $this->redirect(['action' => 'edit', $role->get('name')])(假设name属性是适当的识别作用的实体)。

© www.soinside.com 2019 - 2024. All rights reserved.