【发布时间】:2015-11-18 09:45:05
【问题描述】:
您好,我正在尝试构建我的第一个 RESTful Web 服务,我正在使用来自 lorna jane mitchell 博客的说明。
如果请求来自这个 URL:http://localhost:8888/lorna/index.php/tree/getpath?node_id=75 我调用函数 getpath 传递 node_id 函数获取路径如下所示:
class NestedSet
{
public function getPath($id) {
$sql = "SELECT p." . $this->pk . ", p." . $this->name . " FROM ". $this->table . " n, " . $this->table . " p WHERE n.lft BETWEEN p.lft AND p.rgt AND n." . $this->pk ." = " . $id . " ORDER BY p.lft;";
$result = $this->db->query($sql);
if ($result->num_rows == 0) {
return $this->error(1, true);
}
$path = array();
$i = 0;
while ($row = $result->fetch_assoc()) {
$path[$i] = $row;
$i++;
}
return $path;
}
}
现在我想将此变量 $path 传递给如下所示的 JsonView 类:
class JsonView extends ApiView {
public function render($path) {
header('Content-Type: application/json; charset=utf8');
echo json_encode($path);
return true;
}
}
class ApiView {
protected function addCount($data) {
if(!empty($data)) {
// do nothing, this is added earlier
} else {
$data['meta']['count'] = 0;
}
return $data;
}
}
关于如何通过这个 JsonView 类传递变量 $path 或任何其他变量的任何想法。 非常感谢您的宝贵时间:)
UPDATE 这是创建嵌套类对象的代码
public function getAction($request) {
$data = $request->parameters;
if(isset($request->url_elements[2])) {
switch ($request->url_elements[2]) {
case 'getpath':
$id = $data['node_id'];
$nested = new NestedSet();
$nested->getPath($id);
$api = new JsonView();
$api->render($path);
break;
default:
# code...
break;
}
} else {
$nested = new NestedSet();
echo $nested->treeAsHtml();
}
}
【问题讨论】:
标签: php web-services restful-url restful-architecture