0

使用这个 git-hub 库: http: //github.com/philsturgeon/codeigniter-restserver 如何使用 PUT 功能保存其数据?

example: example.com/put/some-data/some-data/...
4

3 回答 3

0

您可以使用 ajax 请求来完成,例如

(假设使用 jQuery)

$.ajax({
    url: '/index.php/my_controller/somedata',
    type: 'PUT',
    success: function(result) {
        console.log(result);
    }
});
于 2013-01-30T15:54:33.947 回答
0

您可以像这样使用它:但要考虑PUT不太常用且大多数浏览器不支持的计数

function somename_put() 
        {  
            $data = array('somedata: '. $this->put('some-data'));  
            $this->response($data);  
        }  
于 2013-01-30T15:47:38.007 回答
0

根据这个(链接:https ://github.com/philsturgeon/codeigniter-restserver/blob/master/application/libraries/REST_Controller.php#L915 ),$this->put只有在将参数传递给它时才返回(这样才有效:)$username = $this->put('username')。但是在REST_Controller,$this->_put_args中是受保护的,所以你将扩展这个类并可以像这样访问它$params = $this->_put_args

简而言之(这只是一个示例,您可以根据需要对其进行改进);

<?php
// route: /api/users/123
class Users extends REST_Controller
{
    ...

    // update a user's data
    public function user_put() {
        $params = $this->_put_args;

        // you need sanitize input here, "db" is a pseudo
        $username = $db->escape($params['username']);
        $userpass = $db->escape($params['userpass']);

        $db->update(array(
            'username' => $username,
            'userpass' => $userpass
        ), (int) $params['id']);

        if (!$db->error) {
            // suppose right code should be 201 for PUT
            $this->response('Created', 201);
        } else {
            $this->response('Internal Server Error', 500);
        }
    }
}
?>

<script>
// Some ajax library
Ajax("/api/users/123", {
    method: "PUT",
    data: {username:"John", userpass:"new pass"},
    onSuccess: function(){ console.log("Success!"); }
    ...
});
</script>
于 2013-01-30T16:22:04.937 回答