Codeigniter REST API giving unknown method?

淺唱寂寞╮ 提交于 2020-01-04 18:19:50

问题


I'm using https://github.com/chriskacerguis/codeigniter-restserver with codeigniter. I'm trying to add a resource, and enable a get method with an id. I added the Users controller which inherits the REST_Controller. I added a few methods, index_get, index_post. They both worked perfectly.

I then attempted to add an id parameter to the index_get function (so you can access a particular user- eg localhost/proj/Users/4 would you give you the user with id 4)

class Users extends REST_Controller {

    public function index_get($id) {
        echo $id;
    }

    public function index_post() {
       echo "post";
    }

}

I then tried, using postman, to access this get method: localhost/proj/index.php/users/3 but it responded with:

{ "status": false, "error": "Unknown method" }

Any idea how I can fix this?


回答1:


According to CodeIgniter Rest Server doc, you can access request parameter as below :

$this->get('blah'); // GET param 
$this->post('blah'); // POST param 
$this->put('blah'); // PUT param

So, Users class should be like that ..

class Api extends REST_Controller {

    public function user_get() {
        echo $this->get('id');
    }

    public function user_post() {
       echo $this->post('id');
    }
}

When you test with postman, you can request as below :

For get method,

http://localhost/proj/api/user?id=3
http://localhost/proj/api/user/id/3

For post method,

http://localhost/proj/api/user
form-data : [id : 2]

Hope, it will be useful for you.



来源:https://stackoverflow.com/questions/31643133/codeigniter-rest-api-giving-unknown-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!