【问题标题】:Codeigniter RESTful API Server - XML Error?Codeigniter RESTful API 服务器 - XML 错误?
【发布时间】:2015-02-03 15:11:28
【问题描述】:

我已阅读以下主题/教程:

  1. Codeigniter RESTful API Server
  2. http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814
  3. https://github.com/chriskacerguis/codeigniter-restserver

我仍然无法弄清楚为什么我会遇到路由问题和 XML 问题。 我的网络服务控制器在文件夹controllers/api/webservice.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH.'/libraries/RESTful/REST_Controller.php';

class webservice extends REST_Controller{

     function get() {
        $data = array();
        $data['name'] = "TESTNAME";
        $this->response($data); 
     }
}

在教程中不需要添加路由,为了收到 XML 页面错误,我需要添加以下路由,否则它将不起作用:

$route['api/webservice/get'] = 'api/webservice/get';

我的codeIgniter结构文件夹:

 > application
   > config
       rest.php (did not change anything from the GitHub download)
   > controllers
      > api
         webservice.php
         key.php
   > libraries
      > RESTful
         REST_Controller.php (changed line 206 to: $this->load->library('RESTful/format');)
         format.php

根据教程,以下链接在没有路线的情况下有效:

http://localhost/testRestful/index.php/api/example/users

我的只适用于路线

http://localhost/myproject/index.php/api/webservice/get

我收到以下错误: 它没有说别的。我不知道错误在说哪个文件。

【问题讨论】:

  • 这是你的完整控制器代码吗?
  • @Craig 是的。我只是在测试,所以我的控制器中没有其他内容。
  • 我只是问,因为我确定我们以前遇到过这个问题,已通过删除不需要的空白来解决。
  • 我正在尝试删除空行/空格(在文件的头部和底部),看看我是否能找到它
  • @Craig 我已经用这个解决方案github.com/chriskacerguis/codeigniter-restserver/issues/219 解决了 XML 问题,但是路线呢,你知道为什么我需要全部设置吗?

标签: php xml web-services codeigniter rest


【解决方案1】:

如果您使用 REST_Controller,则无法编写 get 函数。 给那个函数名test_get

class webservice extends REST_Controller{
 function test_get() {
    $data = array();
    $data['name'] = "TESTNAME";
    $this->response($data); 
 }
}

现在您可以通过此链接访问该页面

http://localhost/myproject/index.php/api/webservice/test

_get 和 _post 被添加到函数的末尾,以检测它是获取请求还是发布请求。

【讨论】:

  • 你说得对,但是路线呢,我需要全部创建吗?因为在示例中他们没有创建。
  • 顺便说一句,为了 XML 工作 (localhost/myproject/index.php/api/webservice/test?format=xml) 必须将行 ob_get_clean(); 添加到 Format.php construct method
  • 我认为如果你使用正确的路径你不需要路由。你只需要根据请求为每个函数添加 _get 或 _post 。
猜你喜欢
  • 2017-05-14
  • 2013-02-15
  • 2013-06-26
  • 1970-01-01
  • 2018-02-22
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多