12

正如标题所说,我正在使用 Codeigniter 和 phil sturgeon - 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 Resource test 的 FF 插件来发送请求,但是当我使用此 URL 发送 DELETE 请求时http://localhost/api/client/user/id/1,我得到 {"returned from delete:":false}

作为旁注:我找到了这篇文章并使用“X-HTTP-Method-Override”标头并将其作为发布请求发送,我能够获取 id,但我更喜欢客户端不必添加的方式这个标题。

4

2 回答 2

15

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

public function user_delete($id)
{
    $this->response(array(
        'returned from delete:' => $id,
    ));
}
于 2012-06-12T13:25:18.777 回答
0

我有同样的问题,这项工作对我来说

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 )

于 2014-07-24T22:07:37.480 回答