问题
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