Codeigniter RESTful API Server

老子叫甜甜 提交于 2019-12-24 02:18:18

问题


I'm trying to create the RESTful API server in Codeigniter. So far, i followed the instructions i got from here https://github.com/philsturgeon/codeigniter-restserver. So far so good, I created a simple controller to test, so i build a 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 entering 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 issue here?


回答1:


$data has not previously been 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 according to the example, you might use an array instead:

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


来源:https://stackoverflow.com/questions/21089645/codeigniter-restful-api-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!