3

I've read the following topics/tutorials:

  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

And I still can't figure out why I'm with route problems and XML problems. My webservice controller is in the folder 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); 
     }
}

In the tutorials there is no need to add routes, and in order to receive the XML page error I needed to add the following route or it wouldn't work:

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

My structure folder of 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

From the tutorial, the following link works without routes:

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

Mine only works with route

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

And I receive the following error: enter image description here It does not say anything else. I can't figure out which file is the error talking about.

5
  • Is that your full controller code? Commented Dec 5, 2014 at 12:43
  • @Craig Yes it is. I'm just testing so I've nothing else in my controller. Commented Dec 5, 2014 at 13:59
  • I only ask because I am sure we've had this problem before, which was fixed by removed unwanted white space. Commented Dec 5, 2014 at 14:11
  • I'm trying to delete empty lines/spaces (in the head of files and bottom) to see if I can find it Commented Dec 5, 2014 at 14:13
  • @Craig I've solved the XML issue with this solution github.com/chriskacerguis/codeigniter-restserver/issues/219, but what about the routes, do you have any idea why do I need to set them all? Commented Dec 5, 2014 at 14:25

1 Answer 1

1

you cannot write get function if you use REST_Controller. give that function name test_get.

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

Now you can access the page with this link

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

_get and _post is added end of the function to detect either it is get request or post requerst.

Sign up to request clarification or add additional context in comments.

3 Comments

You're absolutely right, but what about the routes, do I need to create them all? Because in the examples they do not create.
Btw, in order to XML works (localhost/myproject/index.php/api/webservice/test?format=xml) the line ob_get_clean(); must be added to Format.php construct method
I think if you use the right path you don't need routes.You just need to add _get or _post to every function depending on request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.