Codeigniter RESTful API Server
I am trying to create a RESTful API server in Codeigniter. So far, I have followed the instructions I received from here https://github.com/philsturgeon/codeigniter-restserver
. So far so good, I created a simple controller for testing, so I create hello.php:
<?php
include(APPPATH.'libraries/REST_Controller.php');
class Hello extends REST_Controller {
function world_get() {
$data->name = "TESTNAME";
$this->response($data);
}
}
?>
When I try to run it by typing http://localhost/peojects/ci/index.php/hello/world
, I get the error:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: Creating default object from empty value</p>
<p>Filename: controllers/hello.php</p>
<p>Line Number: 7</p>
</div>{"name":"TESTNAME"}
What is the problem?
source to share
$data
not previously set / defined. You would need to do something like: $data = new StdObject;
and then assign properties to it:$data -> name = "TESTNAME";
class Hello extends REST_Controller {
function world_get() {
$data = new StdObject;
$data->name = "TESTNAME";
$this->response($data);
}
}
Or, as per example, you can use an array:
class Hello extends REST_Controller {
function world_get() {
$data = array();
$data['name'] = "TESTNAME";
$this->response($data);
}
}
source to share