如何将 DELETE 请求发送到 phil sturgeon's - codeigniter-restserve


How to send DELETE request to phil sturgeon's - codeigniter-restserver

正如标题所说,我正在使用带有 phil sturgeon 的 Codeigniter - codeigniter-restserver 框架。

我已经按照 Nettus 上的教程进行操作,除了发送 DELETE 请求外,一切正常。

法典:

<?php
require(APPPATH.'libraries/REST_Controller.php');
class Client extends REST_Controller{
function user_get()
{
    $data = array('returned:'=> $this->get('id'));
    $this->response($data);
}
function user_post()
{
    $data = array('returned:'=> $this->post('id'));
    $this->response($data);
}
function user_put()
{
    $data = array('returned:'=> $this->put('id'));
    $this->response($data);
}
function user_delete()
{
    $data = array('returned from delete:'=> $this->delete('id'));
    $this->response($data);
}
}

我正在使用一个名为 HTTP 资源测试的 FF 插件来发送请求,但是当我使用以下 URL 发送 DELETE 请求时:http://localhost/api/client/user/id/1 ,我得到 {"从删除返回:":false}

作为旁注:我找到了这篇文章并使用"X-HTTP-Method-Override"标头并将其作为帖子请求发送,我能够获得id,但是我提供了一种客户端不必添加此标头的方法。

根据 HTTP 规范,DELETE 无法发送参数。它可以在 URL 中包含内容,因此您可以将其更改为:

public function user_delete($id)
{
    $this->response(array(
        'returned from delete:' => $id,
    ));
}

我在这项工作中遇到了同样的问题

1 -> REST_Controller.php 将默认的 _parse_delete() 函数替换为:

protected function _parse_delete()
{
    $this->_delete_args = $_DELETE;
    $this->request->format and $this->request->body = file_get_contents('php://input');
    // Set up out DELETE variables (which shouldn't really exist, but sssh!)
    parse_str(file_get_contents('php://input'), $this->_delete_args);
}

仅此而已!不再需要" user_delete($id "