【问题标题】:Cannot update records in CakePHP 3.8无法更新 CakePHP 3.8 中的记录
【发布时间】:2020-03-05 04:34:41
【问题描述】:

我的问题是我无法更新我的 CakePHP 3.8 应用程序中的记录,它安装在实时服务器上。在我的开发服务器上它工作正常。

我可以插入新的,我可以删除,我可以选择,但我无法更新任何记录。

(当然,MySQL用户有权更新记录。)

这就是编辑操作的工作原理,以及从未到达的部分代码:

public function edit($id = null)
    {
        $activity = $this->Activities->get($id, [
            'contain' => []
        ]);
    if ($this->request->is(['patch', 'post', 'put'])) {

        // **** THIS PART OF CODE IS NOT EXECUTED AFTER UPDATE

        $activity = $this->Activities->patchEntity($activity, $this->request->getData());
        if ($this->Activities->save($activity)) {
            $this->Flash->success(__('The {0} has been saved.', 'Activity'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The {0} could not be saved. Please, try again.', 'Activity'));
    }
    $lodges = $this->Activities->Lodges->find('list', ['limit' => 200]);
    $clients = $this->Activities->Clients->find('list', ['limit' => 200]);
    $this->set(compact('activity', 'lodges', 'clients'));
}

我检查了pr($this->request),在尝试更新记录时没有“patch”、“post”或“put”方法,只有“get”。但为什么要“得到”?

CakePHP 3.8 的 CSRF 组件有什么问题吗?

你能帮我解决这个问题吗?

提前谢谢你!

已更新:这是我的表格:

<?php echo $this->Form->create($activity, ['role' => 'form']); ?>  
  <div class="box-body">
    <?php
      echo $this->Form->control('name');
      echo $this->Form->control('description');
      echo $this->Form->control('price_1');
      echo $this->Form->control('price_1_name');
      echo $this->Form->control('price_2');
      echo $this->Form->control('price_2_name');
    ?>
    </div>
    <!-- /.box-body -->
    <?php echo $this->Form->submit(__('Submit')); ?>
    <?php echo $this->Form->end(); ?>

此外,在实时服务器上,此应用程序中的每个更新表单都有相同的问题。在开发服务器上它工作正常。

更新 2:

在 mod 安全日志文件中,我收到以下消息:

请求:POST /locations/edit/3 操作说明:访问被拒绝,代码为 403(阶段 2)。 理由:字符串匹配 REQUEST_COOKIES_NAMES:CAKEPHP 处的“cakephp”。

我在 bootstrap.php 和 app.php 中添加了以下行,但错误信息保持不变:

   Configure::write('Session', [
        'defaults' => 'php',
        'cookie'=>'NICK',
    ]);

【问题讨论】:

  • 您似乎通过 get 访问操作,这是通过 url 中的参数。为了达到 if ($this->request->is(['patch', 'post', 'put'])) 部分,您需要确保您使用的是 http 的 patch/post/put 方法(fe 使用带有发布数据的表格)。见developer.mozilla.org/en-US/docs/Web/HTTP/Methods
  • 这是 - 它是“正常”的蛋糕形式,由 cake 命令烘焙。另外,就像我说的,这适用于我的本地服务器,但不适用于现场。所以我猜这是实时服务器上未启用或配置错误的东西。我在我的主要帖子中添加了我的表单的外观。

标签: cakephp cakephp-3.x cakephp3 cakephp-3.8


【解决方案1】:

所以,解决办法是这样的:

是的,就像我写的那样,我需要在 app.php 中添加下一个:

Configure::write('Session', [
'defaults' => 'php',
'cookie'=>'NICK',
]);

然后删除浏览器中的 cookie。

希望它对某人有所帮助,我真的很难解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多