cakePHP注册don';行不通
我是CakePHP新手,只是从烹饪书的例子开始。现在我想用我的custome表列创建一个简单的注册和登录表单。但是系统不想插入数据!我的错在哪里?在AppController中告诉他要使用哪些字段对吗 add.ctp=cakePHP注册don';行不通,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,我是CakePHP新手,只是从烹饪书的例子开始。现在我想用我的custome表列创建一个简单的注册和登录表单。但是系统不想插入数据!我的错在哪里?在AppController中告诉他要使用哪些字段对吗 add.ctp= <div class="users_form"> <?= $this->Form->create($user) ?> <fieldset> <legend><?= __('Add') ?></l
<div class="users_form">
<?= $this->Form->create($user) ?>
<fieldset>
<legend><?= __('Add') ?></legend>
<?= $this->Form->input('username') ?>
<?= $this->Form->input('password') ?>
</fieldset>
<?= $this->Form->button(__('Submit')); ?>
<?= $this->Form->end() ?>
AppController.php
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['username' => 'ab_mail', 'password' => 'ab_pass']
]
]
]);
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post'))
{
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user))
{
$this->Flash->success(__('inserted!'));
return $this->redirect(['action' => 'add']);
}
$this->Flash->error(__('Error!'));
}
$this->set('user', $user);
}
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['username' => 'username', 'password' => 'password'] // Password and Username fields are pre-define in cakephp.
]
]
]);
UsersController.php
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['username' => 'ab_mail', 'password' => 'ab_pass']
]
]
]);
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post'))
{
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user))
{
$this->Flash->success(__('inserted!'));
return $this->redirect(['action' => 'add']);
}
$this->Flash->error(__('Error!'));
}
$this->set('user', $user);
}
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['username' => 'username', 'password' => 'password'] // Password and Username fields are pre-define in cakephp.
]
]
]);
将视图中的表单元素更改为:
<?= $this->Form->input('ab_mail') ?>
<?= $this->Form->input('ab_pass') ?>
将视图中的表单元素更改为:
<?= $this->Form->input('ab_mail') ?>
<?= $this->Form->input('ab_pass') ?>
需要在AppController.php中进行更改
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['username' => 'ab_mail', 'password' => 'ab_pass']
]
]
]);
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post'))
{
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user))
{
$this->Flash->success(__('inserted!'));
return $this->redirect(['action' => 'add']);
}
$this->Flash->error(__('Error!'));
}
$this->set('user', $user);
}
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['username' => 'username', 'password' => 'password'] // Password and Username fields are pre-define in cakephp.
]
]
]);
这肯定会奏效的
谢谢:)需要在AppController.php中进行更改
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['username' => 'ab_mail', 'password' => 'ab_pass']
]
]
]);
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post'))
{
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user))
{
$this->Flash->success(__('inserted!'));
return $this->redirect(['action' => 'add']);
}
$this->Flash->error(__('Error!'));
}
$this->set('user', $user);
}
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['username' => 'username', 'password' => 'password'] // Password and Username fields are pre-define in cakephp.
]
]
]);
这肯定会奏效的
谢谢:)已修复。如果有人对解决方案感兴趣,只需添加一条注释。已修复。如果有人对解决方案感兴趣,只需添加评论即可。