我在用着https://github.com/chriskacerguis/codeigniter-restserver https://github.com/chriskacerguis/codeigniter-restserver与代码点火器。
我正在尝试添加资源,并启用带有 id 的 get 方法。
我添加了继承 REST_Controller 的 Users 控制器。我添加了几个方法,index_get,index_post。他们都工作得很好。
然后我尝试向 index_get 函数添加一个 id 参数(这样您就可以访问特定用户 - 例如localhost/proj/Users/4
你能给你id为4的用户吗
class Users extends REST_Controller {
public function index_get($id) {
echo $id;
}
public function index_post() {
echo "post";
}
}
然后我尝试使用邮递员访问此 get 方法:localhost/proj/index.php/users/3
但它的回应是:
{ "status": false, "error": "未知方法" }
知道我该如何解决这个问题吗?
根据CodeIgniter Rest 服务器文档 https://github.com/chriskacerguis/codeigniter-restserver/blob/master/README.md#handling-requests,您可以按如下方式访问请求参数:
$this->get('blah'); // GET param
$this->post('blah'); // POST param
$this->put('blah'); // PUT param
所以,用户类应该是这样的..
class Api extends REST_Controller {
public function user_get() {
echo $this->get('id');
}
public function user_post() {
echo $this->post('id');
}
}
当您使用邮递员进行测试时,您可以请求如下:
对于获取方法,
http://localhost/proj/api/user?id=3
http://localhost/proj/api/user/id/3
对于邮寄方法,
http://localhost/proj/api/user
form-data : [id : 2]
希望它对您有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)