我在 CakePHP 中使用 Cupcake Forum 插件。有一个表格用于选择所需的帖子,然后提交表格以删除帖子。表单数据显然正在同时使用 POST 和 GET 方法发送到“主题”控制器中的“温和”功能。该函数首先检查发送的数据是否为 POST。但是,当接收到数据时,它显示它是 GET。一个程序员和我不想完全改变别人的内部代码,但我们无法弄清楚数据是如何通过这两种方法发送并作为 GET 接收的。该插件的代码如下:
--------------moderate.ctp(查看)----------
<?php echo $form->create('Post', array('url' => array('controller' => 'topics', 'action' => 'moderate', $topic['Topic']['slug']))); ?>
-------------topics_controller.php(控制器)--------
public function moderate($id) {
if ($this->RequestHandler->isGet()){
$this->log('Is GET!');
}
$user_id = $this->Auth->user('id');
$topic = $this->Topic->getTopicForViewing($id, $user_id, 'id');
// Access
$this->Toolbar->verifyAccess(array(
'exists' => $topic,
'permission' => $topic['ForumCategory']['accessRead'],
'moderate' => $topic['Topic']['forum_category_id']
));
$this->log('ID: '.$id.'\n');
if ($this->RequestHandler->isPost()){
$this->log('Is POST!');
}
if ($this->RequestHandler->isGet()){
$this->log('Is GET!');
}
$this->log($this->RequestHandler->getReferer());
$this->log(serialize($this->data));
// Processing
if ($this->RequestHandler->isPost()) {
$this->log('INSIDE POST!');
if (!empty($this->data['Post']['items'])) {
$items = $this->data['Post']['items'];
$action = $this->data['Post']['action'];
foreach ($items as $post_id) {
$this->log('Action: '.$action.'\n');
$this->log('PostID: '.$post_id.'\n');
if (is_numeric($post_id)) {
if ($action == 'delete') {
$this->Topic->Post->destroy($post_id);
$this->Session->setFlash(sprintf(__d('forum', 'A total of %d post(s) have been permanently deleted', true), count($items)));
}
}
}
}
}
我们添加了日志检查,显示“Is GET!”的结果 在 Cake 的日志文件中。由于方法是 GET,因此语句 'if ($this->RequestHandler->isPost())' 永远不会为真;因此,提交的帖子不会被删除。我们缺少什么?