我是cakephp的初学者 我的任务是创建restfull json web服务 我从帖子表开始了 给我链接到基本的json webservice 我用谷歌搜索,但有些是以前的版本 一些是xml 我没有开始 我有cakephp 2.4版本
目前 我的控制器是
<?php
class PostsController extends AppController {
public function index() {
//$posts= $this->Post->find('all');
$this->set('posts', $this->Post->find('all'));
//$this->set($posts);
}
public function view($id = null)
{
if(!$id)
{
throw new NotFoundException(__('View Not Found'));
}
$posts = $this->Post->findById($id);
if(!$posts)
{
throw new NotFoundException(__('View Not Found'));
}
$this->set(compact('posts', 'posts'));
}
}
查看
<?php
echo json_encode(compact('posts', 'posts'));
?>
1-它在默认的html模板中显示,我需要纯粹的json 2-它显示每个记录在Post数组中,不需要子数组 就像这样显示
{"posts":{"Post":{"id":"1","title":"The title","body":"This is the post body.","created":"2014-02-08 00:35:50","modified":null}}}
虽然我需要
{"posts":[{"id":"1","title":"The title","body":"This is the post body.","created":"2014-02-08 00:35:50","modified":null}},{"id":"2","title":"A title once again","body":"And the post body follows.","created":"2014-02-08 00:35:50","modified":null}]}