苗条的 JSON 输出 [英] Slim JSON Outputs

查看:36
本文介绍了苗条的 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 框架

这篇关于苗条的 JSON 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆