我正在尝试在Codeigniter中创建RESTful API服务器。到目前为止,我按照我从https://github.com/philsturgeon/codeigniter-restserver
获得的指示。到目前为止一切顺利,我创建了一个简单的控制器来测试,所以我构建了一个hello.php:
<?php
include(APPPATH.'libraries/REST_Controller.php');
class Hello extends REST_Controller {
function world_get() {
$data->name = "TESTNAME";
$this->response($data);
}
}
?>
当我尝试通过输入http://localhost/peojects/ci/index.php/hello/world
来运行它时,我收到错误:
<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"}
这里有什么问题?
答案 0 :(得分:5)
$data
。您需要执行以下操作:$data = new StdObject;
然后为其指定属性:$data -> name = "TESTNAME";
class Hello extends REST_Controller {
function world_get() {
$data = new StdObject;
$data->name = "TESTNAME";
$this->response($data);
}
}
或者根据示例,您可以改为使用数组:
class Hello extends REST_Controller {
function world_get() {
$data = array();
$data['name'] = "TESTNAME";
$this->response($data);
}
}