【发布时间】:2015-05-28 17:22:31
【问题描述】:
我正在使用 cakePHP 构建一个应用程序(我通常是编码新手),我想在一个视图(edit.ctp)上有多个表单,其中表单使用单独的控制器功能。我环顾四周,不知道如何实现这一点,因为一个控制器功能几乎与与控制器方法同名的视图文件相关联。这是我的控制器方法:
public function edit($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid User'));
}
if(!$user) {
throw new NotFoundException(__('Invalid User'));
}
$user = $this->Users->get($id);
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
public function editProfile($id = null)
{
$user = $this->Users->get($id);
if (!$id) {
throw new NotFoundException(__('Invalid User'));
}
if(!$user) {
throw new NotFoundException(__('Invalid User'));
}
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('Your profile has been editted.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Your profile could not be editted. Please, try again.'));
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
$this->view = ('edit');
}
public function changePassword()
{
if (!$id) {
throw new NotFoundException(__('Invalid User'));
}
$password = $this->Users->get($id);
if(!$password) {
throw new NotFoundException(__('Invalid User'));
}
if ($this->request->is(['patch', 'post', 'put'])) {
if ( password_verify($this->request->data('old_password'), $password->password) === true && password_verify($this->request->data('password'), $password->password) === false ) {
$password = $this->Users->patchEntity($password, $this->request->data);
if ($this->Users->save($password)) {
$this->Flash->success(__('Your profile has been editted.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Your profile could not be editted. Please, try again.'));
} elseif ( password_verify($this->request->data('old_password'), $password->password) === false ) {
$this->Flash->error(__('Your old password is not correct.'));
} elseif ( password_verify($this->request->data('old_password'), $password->password) === true ) {
$this->Flash->error(__('You must enter a password different from your current one.'));
} else {
$this->Flash->error(__('Your profile could not be editted. Please, try again.'));
}
}
$this->set(compact('password'));
$this->set('_serialize', ['password']);
$this->view = ('edit');
}
这是我的看法:
//users/edit.ctp
<div class="page-wrap">
<div class="form">
<h1>Edit Your Profile</h1>
<?= $this->Form->create(null, array('url'=>array('controller' => 'users', 'action' => 'editProfile'))) ?>
<?= $this->Form->input('full_name', ['value' => $user->full_name]) ?>
<?= $this->Form->input('email', ['value' => $user->email]) ?>
<?= $this->Form->button(__('Save')) ?>
<?= $this->Form->end() ?>
<?= $this->Form->create(null, array('url'=>array('controller' => 'users', 'action' => 'changePassword'))) ?>
<fieldset>
<legend>Change Your Password</legend>
<?= $this->Form->input('old_password', ['type' => 'password', 'value' => '']) ?>
<?= $this->Form->input('password', ['label' => 'New Password', 'type' => 'password', 'value' => '']) ?>
<?= $this->Form->input('password_confirmation', ['label' => 'New Password Confirmation', 'type' => 'password', 'value' => '']) ?>
</fieldset>
<?= $this->Form->button(__('Change Password')) ?>
<?= $this->Form->end() ?>
<p><?= $this->Html->link('Back', ['controller' => 'users', 'action' => 'index']); ?></p>
</div>
</div>
主要问题是 $user/$password 变量没有从 editProfile 和 changePassword 方法提交到 edit.ctp 视图,所以它会抛出未定义变量的错误,所以我尝试在编辑函数中设置它它消除了错误,但我无法保存信息。其他方法没有获取提交数据的id(在表“users”中找不到主键[NULL]的记录)
【问题讨论】:
标签: php forms cakephp view controller