Reputation: 1784
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?
Upvotes: 3
Views: 10233
Reputation: 3
I've such trouble and this solution worked for me.
For example, you have a controller file called Api.php like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use chriskacerguis\RestServer\RestController;
class Api extends RestController {
function __construct() {
parent::__construct();
}
function user_get($id) {
echo $id;
}
function user_put() {
}
function user_post() {
}
function user_delete() {
}
}
/* End of file Api.php */
/* Location: ./application/controllers/Api.php */
On the browser, you don't need to write http://localhost/api/user_get/1, instead, http://localhost/api/user/1, where 1 is [:id,] because the word _get, _put, _post, or _delete after the word user is the method. So, if you're using the get method, you should write your function in a class like this eg. user_get, users_get, students_get, etc.
Hope it can solve your issue.
Upvotes: 0
Reputation: 3584
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.
Upvotes: 5