When we wrote API, those controllers need to implement the following feature:

1. return JSON format data

2. sometimes support JSONP format data also.

3. support stupid low version IE

If we set format with Yii2, the low version IE will download the response content as a file.

Yii::$app->response->format = Response::FORMAT_JSON

  

So I wrote a component for it, any API controller just need to extend it.

 namespace api\components;

 use Yii;
use yii\web\Response; class Controller extends \yii\web\Controller
{
protected $isLowIE;
protected $callback; public function beforeAction($action)
{
$this->layout = false;
$this->callback = Yii::$app->request->get('callback', false); $this->isLowIE = (boolean)Yii::$app->request->get('ie', false); if (!$this->isLowIE && !$this->callback) {
Yii::$app->response->format = Response::FORMAT_JSON;
} return parent::beforeAction($action);
} /**
* @param \yii\base\Action $action
* @param mixed $result
* @return mixed
*/
public function afterAction($action, $result)
{
$result = parent::afterAction($action, $result);
// your custom code here
if ($this->isLowIE || $this->callback) {
$result = json_encode($result); if ($this->callback) {
$result = $this->callback .'('. $result .')';
}
}
return $result;
}
}

Yii2 components api/controller的更多相关文章

  1. Yii2 restful api创建,认证授权以及速率控制

    Yii2 restful api创建,认证授权以及速率控制 下面是对restful从创建到速率控制的一个详细流程介绍,里面的步骤以及截图尽可能详细,熟悉restful的盆友可能觉得过于繁琐,新手不妨耐 ...

  2. Yii2 Restful API 原理分析

    Yii2 有个很重要的特性是对 Restful API的默认支持, 通过短短的几个配置就可以实现简单的对现有Model的RESTful API 参考另一篇文章: http://www.cnblogs. ...

  3. yii2 RESTful API 405 Method Not Allowed

    关于 Yii2 中 RESTful API 的开发,可以参考另一篇随笔 http://www.cnblogs.com/ganiks/p/yii2-restful-api-dev.html 测试的过程中 ...

  4. 重构Web Api程序(Api Controller和Entity)续篇

    昨天有写总结<重构Web Api程序(Api Controller和Entity)>http://www.cnblogs.com/insus/p/4350111.html,把一些数据交换的 ...

  5. MVC Controller 链接到 API Controller 以及反向链接

    MVC Controller 链接到 API Controller 以及反向链接 问题 想创建一个从 ASP.NET MVC controller 到 ASP.NET Web API controll ...

  6. Yii2 Restful Api 401

    采用Yii2 Restful Api方式为APP提供数据,默认你已经做好了所有的编码和配置工作.采用Postman测试接口: 出现这个画面的一个可能原因是:access_token的写法有误,如果你使 ...

  7. 测试 ASP.NET Core API Controller

    本文需要您了解ASP.NET Core MVC/Web API, xUnit以及Moq相关知识. 这里有xUnit和Moq的介绍: https://www.cnblogs.com/cgzl/p/917 ...

  8. There is no action xxxFun defined for api controller api/subitem

    在使用abp的框架时,访问某个接口方法出现错误: There is no action xxxFun defined for api controller api/subitem 原因:肯定是访问的接 ...

  9. Only one complex type allowed as argument to a web api controller action.

    错误内容: message":"An error has occurred.","exceptionMessage":"Only one c ...

随机推荐

  1. 1.8 收集的XSS Payload

    收集的XSS Payload ,可以做成字典,到时候批量测试:--------------------------------------------------------------------- ...

  2. ubuntu-12.04.5安装cacti笔记

    坑啊,磨磨蹭蹭按了一个星期.按了3个版本. 第一次:cacti-0.8.7e.tar.gz 安装完之后,Host: Localhost->Memory Usage...四张图始终出不了.点击进去 ...

  3. 浅谈REST API

    浅谈REST API 说明: 本文部分内容根据其它网络文章编写,如有版权问题请及时通知. 背景 发迹于互联网的REST,在国内国外混得可谓是风生水起,如今又进入电信行业的视野,连TMF都将其作为战略项 ...

  4. IOHelper(自制常用的输入输出的帮助类)

    常用的读写文件,和地址转换(win和linux均支持),操作文件再也不是拼接那么的low了 using System; using System.Diagnostics; using System.I ...

  5. ASP.NET MVC 小牛之旅2:体验第一个MVC程序

    了解了什么是MVC之后,接下来用一个非常简单的留言板程序概要的了解MVC网站开发的过程,对MVC开发有个大致的轮廓.第一个项目将不会提到过多与数据库相关的技术,因此将以Framework Code F ...

  6. 痞子衡嵌入式:恩智浦i.MX RTxxx系列MCU特性介绍(2)- RT685EVKA性能实测(Dhrystone)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是恩智浦i.MX RTxxx系列MCU的性能. 在前面的文章 i.MXRTxxx微控制器概览 里,痞子衡给大家简介过恩智浦半导体在2018 ...

  7. ejs使用文档

    EJS是一个javascript模板库,用来从json数据中生成HTML字符串. 功能:缓存功能,能够缓存好的HTML模板: <% code %>用来执行javascript代码 ejs模 ...

  8. springmvc ajax 简单例子

    1.控制器曾 @Controller public class AjaxController { @RequestMapping("/ajax") public void ajax ...

  9. python_sting字符串的方法及注释

    string类型是python内置的类型,无需安装   方法/属性 说明   capitalize()   把字符串的第一个字符改为大写   casefold()   把整个字符串的所有字符改为小写 ...

  10. Ubuntu apt-get update中断的时候会出现一个错误导致不能再apt-get update

    错误描述为:Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable) E: Un ...