【发布时间】:2013-01-25 02:45:23
【问题描述】:
我在我的 cakephp 2.3.0 中为 json 设置了 parseExtension。不显示错误。有效吗?
我该如何测试?
在 RoR 中很容易测试通过
https://mydomain.de/view/4.json
如何在 cakephp 上运行?
我的 View-Action 是这样的。
public function view($id = null) {
if (!$this->Atla->exists($id)) {
throw new NotFoundException(__('Invalid atla'));
}
$options = array('conditions' => array('Atla.' . $this->Atla->primaryKey => $id));
$this->set('atla', $this->Atla->find('first', $options));
$this->Atla->id = $id;
$result = $this->Atla->read();
$this->response->type('json');
$this->response->body(json_encode($result));
return $this->response;
$this->set(compact('atlas'));
}
为什么我总是收到 json 请求?
【问题讨论】:
-
如果我在浏览器中输入这个,我会收到错误
{"code":500,"url":"\/Atlas\/view\/2.json","name":"View file "\/usr\/local\/www\/cakephp\/app\/View\/Atlas\/json\/view.ctp" is missing."} -
那么它似乎工作了!您所要做的就是创建视图
app/View/Atlas/json/view.ctp,这是用于 .json 请求的视图。 没有 .json 的请求将使用app/View/Atlas/view.ctp:) 这里有更多关于创建/使用 JSON 和 XML 视图的信息:book.cakephp.org/2.0/en/views/json-and-xml-views.html -
这是一个普通的(c&p)视图文件还是应该是空的?
-
我在下面的答案中做了解释,因为 cmets 没有提供太多选项来格式化代码并保持可读性。希望对你有帮助
标签: php cakephp controller components cakephp-2.3