如何在 yii 中获取 json 格式(application/json)的响应?
如何在 yii 中获取 json 格式(application/json)的响应?
How to get response as json format(application/json) in yii?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
对于 Yii 1:
在您的(基础)控制器中创建此函数:
然后只需在操作结束时调用:
对于 Yii 2:
Yii 2 内置了此功能,在控制器操作末尾使用以下代码:
For Yii 1:
Create this function in your (base) Controller:
Then simply call at the end of your action:
For Yii 2:
Yii 2 has this functionality built-in, use the following code at the end of your controller action:
对于控制器内的 Yii2:
For Yii2 inside a controller:
使用
示例代码的一种更简单的方法:
干杯:)
one more simple way by using
example code:
cheers :)
对于 Yii2 使用这个简单易记的选项
for Yii2 use this simple to remember option
在要渲染 JSON 数据的控制器操作中,例如:actionJson()
请参阅更多 Yii API
In the controller action that you want to render JSON data, e.g: actionJson()
See more Yii API
我认为这个解决方案不是结束应用程序流程的最佳方式,因为它使用了 PHP 的
exit()
函数,这意味着立即退出执行流程。是的,有 Yii 的onEndRequest
处理程序和 PHP 的register_shutdown_function
但它仍然太宿命论了。对我来说,更好的方法是这样
,因此,应用程序流程即使在之后仍然继续执行。
I think this solution is not the best way to end application flow, because it uses PHP's
exit()
function, witch means immediate exit from execution flow. Yes, there is Yii'sonEndRequest
handler, and PHP'sregister_shutdown_function
but it still remains too fatalistic.For me the better way is this
So, the application flow continues to execute even after.