猿问

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

控制器代码:


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

?>

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


守着一只汪
浏览 93回答 1
1回答

阿波罗的战车

要获取发布数据,请使用调试debug($this->request->getData());&nbsp;看起来您需要所有 CRUD 代码。因此,您可以在下面创建您的用户 add.ctp 文件位置:模板\用户\add.ctp<?= $this->Form->create($user) ?><?php&nbsp; &nbsp;echo $this->Form->control('username');&nbsp; &nbsp;echo $this->Form->control('email');&nbsp; &nbsp;echo $this->Form->control('password');?><?= $this->Form->button(__('Submit')) ?><?= $this->Form->end() ?>然后用户控制器看起来像&nbsp;UsersController/add method look like&nbsp;&nbsp;public function add()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $user = $this->Users->newEmptyEntity();&nbsp; &nbsp; &nbsp; &nbsp; if ($this->request->is('post')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $user = $this->Users->patchEntity($user, $this->request->getData());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($this->Users->save($user)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->Flash->success(__('The user has been saved.'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this->redirect(['action' => 'index']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->Flash->error(__('The user could not be saved. Please, try again.'));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $this->set(compact('user'));&nbsp; &nbsp; }你需要更多吗?只需要在你的应用文件夹中给出一个简单的命令来生成你的 CRUD。发号施令bin/cake bake all users假设,users 是你的数据库表名。然后你会得到添加,编辑,删除,查看。 可以看这个教程看看如何给蛋糕烘烤命令
随时随地看视频慕课网APP
我要回答