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. ES Docs-1:Installation Elasticsearch-2.3.1

    installation Elasticsearch requires at least Java 7. Specifically as of this writing, it is recommen ...

  2. 配置OpenCV报应用程序无法正常启动0xc000007b

    我的配置软件是OpenCV3.4.1和visual studio2017.参考这篇博客(https://blog.csdn.net/qq_41175905/article/details/805604 ...

  3. linux系统elementray os的环境搭建

    因为我在使用过程中为了改变终端的外表,结果把/ect/psswd,以及/ect/profile中的文件配置修改之后,我把gnome-terminal的python脚本打包放在/bin/目录下,修改了/ ...

  4. HELLO---MVC

    前言 很荣幸有机会参加BS的项目,这个图书馆系统这个项目,需要用到ITOO框架,其中涉及到好多小框架的学习,MVC就是其中的一个学习知识点,像大家一样,刚刚接触一个新鲜的知识,心里除了恐惧还有就是茫然 ...

  5. hdu6055(求正方形个数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6055 题意: 给出 n 组坐标 x, y, 输出其中的正多边形个数 . 其中 x, y 均为整数. ...

  6. Node.js 内置模块crypto使用事件方法(onreadable)加密的一些问题

    javaScript代码如下: 'use strict'; const crypto = require('crypto'); //实例化一个AES加密对象 const aesEncrept = cr ...

  7. ubuntu命令错误集

    1.在ubuntu命令行使用rz从windows传输文件时出现乱码 解决方法:使用 rz -e    选项进行传输,一般小文件传输不用加 -e 选项,大文件传输需要.

  8. java程序调用.net接口服务地址的写法

    参考文章:http://download.csdn.net/detail/davidiao/7424767 http://www.cnblogs.com/mq0036/p/3554002.html . ...

  9. LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  10. java里如何实现对数组中的元素反转[4, 1, 8, 7, 3, 8, 2]变成 [2, 8, 3, 7, 8, 1, 4]

    不多说,直接上干货! 给定一个数组,对其进行反转. {3,1,6,5,8,2} --> {2,8,5,6,1,3}; 其实就是头尾元素的位置置换. package zhouls.bigdata. ...