Codeigniter REST API提供未知方法? [英] Codeigniter REST API giving unknown method?

查看:77
本文介绍了Codeigniter REST API提供未知方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 https://github.com/chriskacerguis/codeigniter-restserver 与代码点火器。
我正在尝试添加资源,并启用带有ID的get方法。
我添加了继承REST_Controller的Users控制器。我添加了一些方法,index_get,index_post。他们两个都工作完美。

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.

然后,我尝试向id_get函数添加一个id参数(以便您可以访问特定用户,例如 localhost / proj / Users / 4 您会给您ID为4的用户吗?

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";
    }

}

然后我尝试使用邮递员,访问此获取方法:
localhost / proj / index.php / users / 3 ,但响应为:

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


{状态:false,错误:未知方法}

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

知道我该如何解决吗?

推荐答案

根据 CodeIgniter Rest Server文档,您可以访问以下请求参数:

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

因此,Users类应该像这样..

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 :

获取方法,

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

对于发布方法,

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

希望,它将对您有用。

这篇关于Codeigniter REST API提供未知方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆