PUT方法不起作用(CodeIngiter + Codeigniter休息服务器)

时间:2015-10-11 10:42:23

标签: php codeigniter rest

我使用Codeigniter 3.01和Codeigniter rest server
我在put方法上遇到了问题。 当我通过put方法运行http://codeang.si/api/task/id/2时(查看附图)并尝试var_dump($data);我得到这个"空"结果:

array (size=3)
  'task' => boolean false
  'active' => boolean false
  'date_c' => string '2015-10-11 12:10' (length=16) 

这是我的api控制器:

    <?php
require(APPPATH.'libraries/REST_Controller.php');


class Api extends REST_Controller{

    function tasks_get(){
        // respond with information about a task
        $task = $this->tm->get_all();
        if($task){
            $this->response($task, 200);
        }else{
            $this->response(NULL, 404);
        }
    }

    function task_get(){
        // respond with information about a task
        if(!$this->get('id')){
            $this->response(NULL, 400);
        }
        $task = $this->tm->get($this->get('id'));
        if($task){
            $this->response($task, 200);
        }else{
            $this->response(NULL, 404);
        }
    }

    function task_post(){
        // create task
        $data = array(
            'task' => $this->post('task'),
            'active' => $this->post('active'),
            'date_c' => date("Y-m-d h:i:s")
        );
        $result = $this->tm->create( $data);
        if($result === FALSE){
            $this->response(array('status' => 'failed'));
        }else{
            $this->response(array('status' => 'success'));
        }
    }

    function task_put(){
        // update task
        $id = $this->get('id');
        $data = array(
            'task' => $this->put('task'),
            'active' => $this->put('active'),
            'date_u' => date("Y-m-d h:i:s")
        );
        var_dump($data, $id);
        die();
        $result = $this->tm->update($this->get('id'), $data);
        if($result === FALSE){
            $this->response(array('status' => 'failed'));
        }else{
            $this->response(array('status' => 'success'));
        }
    }

    function task_delete(){
        //delete a task and respond with a status/errors
        $result = $this->tm->delete($this->get('id'));
        if($result === FALSE){
            $this->response(array('status' => 'failed'));
        }else{
            $this->response(array('status' => 'success'));
        }
    }

}

这是我的结果请求: Result

我希望有人可以提供帮助。 提前谢谢

2 个答案:

答案 0 :(得分:4)

我找到了解决方案。这不是代码中的问题,而是我的“身体”。 我不得不在Postman chrome扩展中从form-data切换到x-www-form-urlencoded,现在put方法工作正常。

以下是示例图片: enter image description here

答案 1 :(得分:0)

它必须是'task' => $this->input->post('task')等等(你错过了输入)。