我想,当用户提交表单,然后这个表单的内容将被保存在数据库中,但随后会重定向到相同的形式相同,前一个,使用户可以编辑其中的一些,重新保存等领域。我怎样才能做到这一点?
码
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'));
}
对于住在同一页/表单只是删除return $this->redirect(['action' => 'index'])
- 但我不认为这是你真正想要的。用户将死结束了“添加”页面上 - 只需添加更多的实体...
退房的CakePHP Controller tutorial - 它展示了如何创建edit
方法和模板。这样做之后,你必须重定向更改为类似return $this->redirect(['action' => 'edit', $role->get('name')])
(假设name
属性是适当的识别作用的实体)。