超薄JSON输出 [英] Slim JSON Outputs
问题描述
我正在将Slim框架与PHP结合使用,以为我的应用程序创建RESTful API.但是,我认为该框架将具有某种方式来创建更简单的JSON输出,而不仅仅是exit($jsonEncodedVariable);
.
I am using the Slim framework with PHP to create a RESTful API for my app. However, I assumed that the framework would have some way of creating easier JSON outputs rather than just exit($jsonEncodedVariable);
.
我是否缺少框架中的内容,还是我需要为每个方法使用json_encode()
... exit($json)
...?
Am I missing something in the framework, or do I need to use json_encode()
... exit($json)
... for every method?
所有数据都从我的MySQL数据库中取出,然后将根据调用的REST请求放入JSON数组中.
All of the data is taken out of the my MySQL database and would then be put into a JSON array depending on what REST request was called.
例如,如果请求/api/posts/all
,我将exit()
所有帖子的JSON数组,每个帖子都为其自己的键"value" : key
赋值.
For example, if /api/posts/all
was requested, I would exit()
a JSON array of all the posts which each value for its own key, "value" : key
.
我的问题是,有没有一种简单的方法,可以使用苗条的框架来实现exit()
的JSON代码而不是将其作为纯文本退出?
My question is, is there an easy way, using the slim framework, for exit()
'ing JSON code instead of exiting it as plain text?
推荐答案
header("Content-Type: application/json");
echo json_encode($result);
exit;
这篇关于超薄JSON输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!