<?php namespace app\modules\v1\controllers; use Yii; use yii\web\Response; class BaseAPIController extends \yii\rest\Controller { public $serializer = [ ‘class‘ => ‘yii\rest\Serializer‘, ‘collectionEnvelope‘ => ‘items‘, ]; public function behaviors() { $behaviors = parent::behaviors(); $behaviors[‘contentNegotiator‘][‘formats‘] = [ ‘application/json‘ => Response::FORMAT_JSON ]; $behaviors[‘access‘] = [ ‘class‘ => ‘yii\filters\AccessControl‘, ‘rules‘ => [ [ ‘allow‘ => true, ‘matchCallback‘ => function ($rule, $action) { return in_array(Yii::$app->request->hostInfo, Yii::$app->params[‘acceptableHosts‘]); } ] ] ]; $behaviors[‘corsFilter‘] = [ ‘class‘ => ‘yii\filters\Cors‘, ‘cors‘ => [ ‘Origin‘ => Yii::$app->params[‘acceptableHosts‘], ‘Access-Control-Request-Method‘ => [‘GET‘, ‘POST‘, ‘PUT‘, ‘PATCH‘, ‘DELETE‘, ‘HEAD‘, ‘OPTIONS‘], ‘Access-Control-Request-Headers‘ => [‘*‘], ‘Access-Control-Allow-Credentials‘ => true, ‘Access-Control-Max-Age‘ => 86400, ‘Access-Control-Expose-Headers‘ => [], ], ]; return $behaviors; } }
,相关文章
- 在Java中创建RESTful API服务器需要什么?
- 使用Visual Studio Team Services REST API的AJAX跨域问题
- 来自RESTful api的Rails json请求如何呈现视图
- Asp.Net Core 3.0 学习3、Web Api 文件上传 Ajax请求以及跨域问题
- 使用静态基类方案让 ASP.NET Core 实现遵循 HATEOAS Restful Web API
- 使用MVC为iPhone应用程序构建RESTful API - 如何保护它?
- 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】06、Mybatis+SQLServer集成
- 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】05、Shiro集成
- 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】02、创建新的SpringBoot项目
- SpringBoot使用Swagger2搭建强大的RESTful API 文档功能