From 57cb9df4bfd4717a89fe50ea5b569a6bc5699005 Mon Sep 17 00:00:00 2001 From: andrey Date: Mon, 29 Nov 2021 18:21:50 +0300 Subject: [PATCH] api cors --- .../modules/api/controllers/ApiController.php | 30 +++++++++++++++++++ .../api/controllers/ProfileController.php | 26 ++++++---------- .../api/controllers/ReportsController.php | 21 ++++--------- .../api/controllers/SkillsController.php | 26 ++++++---------- .../api/controllers/UserController.php | 26 ++++++++-------- 5 files changed, 66 insertions(+), 63 deletions(-) create mode 100644 frontend/modules/api/controllers/ApiController.php diff --git a/frontend/modules/api/controllers/ApiController.php b/frontend/modules/api/controllers/ApiController.php new file mode 100644 index 0000000..2d0e59e --- /dev/null +++ b/frontend/modules/api/controllers/ApiController.php @@ -0,0 +1,30 @@ + [ + 'class' => GsCors::class, + 'cors' => [ + 'Origin' => ['*'], + //'Access-Control-Allow-Credentials' => true, + 'Access-Control-Allow-Headers' => [ + 'Content-Type', + 'Access-Control-Allow-Headers', + 'Authorization', + 'X-Requested-With' + ], + ] + ], + ]; + } + +} \ No newline at end of file diff --git a/frontend/modules/api/controllers/ProfileController.php b/frontend/modules/api/controllers/ProfileController.php index 4e05e5e..4a7d745 100755 --- a/frontend/modules/api/controllers/ProfileController.php +++ b/frontend/modules/api/controllers/ProfileController.php @@ -12,17 +12,20 @@ use kavalar\TelegramBotService; use yii\filters\auth\CompositeAuth; use yii\filters\auth\HttpBearerAuth; use yii\filters\auth\QueryParamAuth; +use yii\filters\ContentNegotiator; +use yii\web\Response; -class ProfileController extends \yii\rest\Controller +class ProfileController extends ApiController { public function behaviors() { - return [ + $parent = parent::behaviors(); + $b = [ [ - 'class' => \yii\filters\ContentNegotiator::className(), + 'class' => ContentNegotiator::className(), 'formats' => [ - 'application/json' => \yii\web\Response::FORMAT_JSON, + 'application/json' => Response::FORMAT_JSON, ], ], 'authenticator' => [ @@ -31,20 +34,9 @@ class ProfileController extends \yii\rest\Controller HttpBearerAuth::class, ], ] -// 'corsFilter' => [ -// 'class' => GsCors::class, -// 'cors' => [ -// 'Origin' => ['https://itguild.info'], -// //'Access-Control-Allow-Credentials' => true, -// 'Access-Control-Allow-Headers' => [ -// 'Content-Type', -// 'Access-Control-Allow-Headers', -// 'Authorization', -// 'X-Requested-With' -// ], -// ] -// ] ]; + + return array_merge($parent, $b); } public function actionIndex($id = null) diff --git a/frontend/modules/api/controllers/ReportsController.php b/frontend/modules/api/controllers/ReportsController.php index 11333c7..b14be76 100755 --- a/frontend/modules/api/controllers/ReportsController.php +++ b/frontend/modules/api/controllers/ReportsController.php @@ -11,12 +11,11 @@ use Yii; use yii\filters\auth\CompositeAuth; use yii\filters\auth\HttpBearerAuth; use yii\filters\ContentNegotiator; -use yii\rest\Controller; use yii\web\BadRequestHttpException; use yii\web\NotFoundHttpException; use yii\web\Response; -class ReportsController extends Controller +class ReportsController extends ApiController { public function init() { @@ -25,26 +24,14 @@ class ReportsController extends Controller public function behaviors() { - return [ + $parent = parent::behaviors(); + $b = [ [ 'class' => ContentNegotiator::className(), 'formats' => [ 'application/json' => Response::FORMAT_JSON, ], ], -// 'corsFilter' => [ -// 'class' => GsCors::class, -// 'cors' => [ -// 'Origin' => ['*'], -// //'Access-Control-Allow-Credentials' => true, -// 'Access-Control-Allow-Headers' => [ -// 'Content-Type', -// 'Access-Control-Allow-Headers', -// 'Authorization', -// 'X-Requested-With' -// ], -// ] -// ], 'authenticator' => [ 'class' => CompositeAuth::class, 'authMethods' => [ @@ -52,6 +39,8 @@ class ReportsController extends Controller ], ] ]; + + return array_merge($parent, $b); } public function actionIndex(): array diff --git a/frontend/modules/api/controllers/SkillsController.php b/frontend/modules/api/controllers/SkillsController.php index c0bc06f..45a7780 100755 --- a/frontend/modules/api/controllers/SkillsController.php +++ b/frontend/modules/api/controllers/SkillsController.php @@ -8,16 +8,19 @@ use yii\filters\AccessControl; use yii\filters\auth\CompositeAuth; use yii\filters\auth\HttpBearerAuth; use yii\filters\auth\QueryParamAuth; +use yii\filters\ContentNegotiator; +use yii\web\Response; -class SkillsController extends \yii\rest\Controller +class SkillsController extends ApiController { public function behaviors() { - return [ + $parent = parent::behaviors(); + $b = [ [ - 'class' => \yii\filters\ContentNegotiator::className(), + 'class' => ContentNegotiator::className(), 'formats' => [ - 'application/json' => \yii\web\Response::FORMAT_JSON, + 'application/json' => Response::FORMAT_JSON, ], ], 'authenticator' => [ @@ -26,20 +29,9 @@ class SkillsController extends \yii\rest\Controller HttpBearerAuth::class, ], ] -// 'corsFilter' => [ -// 'class' => GsCors::class, -// 'cors' => [ -// 'Origin' => ['*'], -// //'Access-Control-Allow-Credentials' => true, -// 'Access-Control-Allow-Headers' => [ -// 'Content-Type', -// 'Access-Control-Allow-Headers', -// 'Authorization', -// 'X-Requested-With' -// ], -// ] -// ] ]; + + return array_merge($parent, $b); } public function actionIndex() diff --git a/frontend/modules/api/controllers/UserController.php b/frontend/modules/api/controllers/UserController.php index b8a9332..d725bbb 100755 --- a/frontend/modules/api/controllers/UserController.php +++ b/frontend/modules/api/controllers/UserController.php @@ -36,19 +36,19 @@ class UserController extends ActiveController // HttpBearerAuth::class, // ], // ], -// 'corsFilter' => [ -// 'class' => GsCors::class, -// 'cors' => [ -// 'Origin' => ['*'], -// //'Access-Control-Allow-Credentials' => true, -// 'Access-Control-Allow-Headers' => [ -// 'Content-Type', -// 'Access-Control-Allow-Headers', -// 'Authorization', -// 'X-Requested-With' -// ], -// ] -// ] + 'corsFilter' => [ + 'class' => GsCors::class, + 'cors' => [ + 'Origin' => ['*'], + //'Access-Control-Allow-Credentials' => true, + 'Access-Control-Allow-Headers' => [ + 'Content-Type', + 'Access-Control-Allow-Headers', + 'Authorization', + 'X-Requested-With' + ], + ] + ] ]); }