REST服务无法正常工作 - Code Igniter

时间:2013-03-01 17:15:35

标签: php codeigniter rest

我想在我的项目中实现Phill Sturgeon CodeIgniter RESTServer库。
我分别在文件夹 config,library,library 中复制了文件 rest.php,Format.php,REST_Controler.php

我使用以下代码创建了名为services的控制器:

<?php
require(APPPATH.'/libraries/REST_Controller.php');

class services extends REST_Controller {  

    function Teams_get(){

        $teamNames=$this->team_model->getTeamNames();

         $this->response($teamNames);
    }

TeamModel在我的 autoload.php 中自动加载。当我想在浏览器中运行Teams_get方法时,结果是:

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

我读到here我应该更改 REST_Controler.php 配置文件,但只有在POST方法不起作用时才能进行此更改。

我的服务应该是公开的,所以我不需要身份验证方法。

这里有什么问题?

1 个答案:

答案 0 :(得分:16)

调用API时,网址应该只是方法的名称,而不是_get(或_post)。这是由REST服务器添加的,具体取决于URL的调用方式(GET vs POST)。

因此,要调用Teams_get方法,您需要向网址GET发送/services/Teams个请求(不是/services/Teams_get)。

文档:https://github.com/philsturgeon/codeigniter-restserver#handling-requests

相关问题