是否可以通过表单提交来保存数据



控制器代码:

public function login()
{
$this->LoadModel('Users');
pr($this->data);
}

查看代码:

<?php echo
$this->Form->create('Post').
$this->Form->control('email').'<br>'.
$this->form->label('password').'<br>'.
$this->Form->password('password').'<br>'.
$this->Form->submit('log in').
$this->Form->end();
?>

我不明白数据已提交给控制器,但要从控制器读取数据。 我得到用控制器保存的数据,但要访问数据变量,即提交表单数据无法读取到控制器。

要获取发布数据,请使用调试

debug($this->request->getData()); 

看起来您需要所有 CRUD 代码。

因此,您可以在下面创建您的用户add.ctp文件

位置 : 模板\用户\添加.ctp

<?= $this->Form->create($user) ?>
<?php
echo $this->Form->control('username');
echo $this->Form->control('email');
echo $this->Form->control('password');
?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

然后用户控制器看起来像

UsersController/add method look like 
public function add()
{
$user = $this->Users->newEmptyEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
$this->set(compact('user'));
}

你需要更多吗? 只需在应用程序文件夹中给出一个简单的命令即可生成 CRUD。

给命令

bin/cake bake all users

假设用户是数据库表名。 然后你会得到添加,编辑,删除,查看。 可以查看本教程以查看如何给出蛋糕烘焙命令

最新更新