苗条的 JSON 输出
我正在使用 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 数组,其中包含所有帖子的每个值对应其自己的键,值":键
.
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
.
我的问题是,有没有一种简单的方法,使用 slim 框架,exit()
'ing 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;
提示:使用 Slim用于开发 REST API 的 PHP 框架
相关文章