预填写多个表单并在一个视图上提交多个控制器功能 cakephp
Pre-filling Multiple Forms and submitting on one view with multiple controller functions cakephp
我正在使用 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(在 table "users" 中找不到主键 [NULL] 的记录)
我找到了一个快速修复方法,方法是将控制器函数 changePassword 变量 $password 更改为 $user,然后我没有将 null 放在 $this->Form->create 中,而是将 $user 放在两种形式中,因为它们是在控制器中提交两个不同的功能它仍然有效。
我正在使用 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(在 table "users" 中找不到主键 [NULL] 的记录)
我找到了一个快速修复方法,方法是将控制器函数 changePassword 变量 $password 更改为 $user,然后我没有将 null 放在 $this->Form->create 中,而是将 $user 放在两种形式中,因为它们是在控制器中提交两个不同的功能它仍然有效。