我想在我的项目中实现 Phill Sturgeon CodeIgniter RESTServer 库。
我分别将文件rest.php、Format.php、REST_Controler.php复制到文件夹config、library、library中。
我使用以下代码创建了名为 services 的 Controller :
<?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":"未知方法。"}
我读到here我应该更改 REST_Controler.php 配置文件,但只有在 POST 方法不起作用时才应进行此更改。
我的服务应该是公开的,所以我不需要身份验证方法。
这里出了什么问题?
最佳答案
调用 API 时,URL 应该只是方法的名称,不带 _get
(或 _post
)。这是由 REST 服务器根据 URL 的调用方式添加的(GET
与 POST
)。
因此,要调用您的 Teams_get
方法,您需要将 GET
请求发送到 URL /services/Teams
(而不是 >/services/Teams_get
)。
文档:https://github.com/philsturgeon/codeigniter-restserver#handling-requests
关于php - REST 服务不工作-Code Igniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15163218/