2014-12-05 135 views
3

我读过以下主题/教程:Codeigniter RESTful API服务器 - XML错误?

  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很问题。 我的web控制器是文件夹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'; 

我笨的结构文件夹:

> 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 

我收到以下错误: enter image description here 它没有说别的。我无法弄清楚哪个文件是错误的。

+0

是你的完整控制器代码吗? – Craig 2014-12-05 12:43:10

+0

@克雷格是的。我只是测试,所以我的控制器中没有其他东西。 – Linesofcode 2014-12-05 13:59:41

+0

我只问,因为我确信我们之前有过这个问题,这是通过删除不需要的空白区域来解决的。 – Craig 2014-12-05 14:11:08

回答

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添加功能的最终检测访问页面要么是get请求或交requerst。

+0

你说的没错,但是路线怎么样,我是否需要全部创建它们?因为在他们不创造的例子。 – Linesofcode 2014-12-05 14:31:15

+0

顺便说一句,为了使XML工作(http://localhost/myproject/index.php/api/webservice/test?format = xml),必须将'ob_get_clean();'行添加到Format.php'construct method' – Linesofcode 2014-12-05 14:33:10

+0

我想如果你使用正确的路径,你不需要routes.You只需要添加_get或_post到每个功能取决于请求。 – 2014-12-05 14:34:43