2014-12-05 133 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