diff --git a/frontend/modules/api/controllers/ApiController.php b/frontend/modules/api/controllers/ApiController.php index 2d0e59e..81fd9f9 100644 --- a/frontend/modules/api/controllers/ApiController.php +++ b/frontend/modules/api/controllers/ApiController.php @@ -3,7 +3,11 @@ namespace frontend\modules\api\controllers; use common\behaviors\GsCors; +use yii\filters\auth\CompositeAuth; +use yii\filters\auth\HttpBearerAuth; +use yii\filters\ContentNegotiator; use yii\rest\Controller; +use yii\web\Response; class ApiController extends Controller { @@ -24,6 +28,18 @@ class ApiController extends Controller ], ] ], + 'authenticator' => [ + 'class' => CompositeAuth::class, + 'authMethods' => [ + HttpBearerAuth::class, + ], + ], + [ + 'class' => ContentNegotiator::className(), + 'formats' => [ + 'application/json' => Response::FORMAT_JSON, + ], + ], ]; } diff --git a/frontend/modules/api/controllers/DocumentController.php b/frontend/modules/api/controllers/DocumentController.php index b0ebf4b..49e11bd 100644 --- a/frontend/modules/api/controllers/DocumentController.php +++ b/frontend/modules/api/controllers/DocumentController.php @@ -14,18 +14,8 @@ use yii\web\NotFoundHttpException; use yii\rest\Controller; use yii\web\ServerErrorHttpException; -class DocumentController extends Controller +class DocumentController extends ApiController { - public function behaviors(): array - { - $behaviors = parent::behaviors(); - - $behaviors['authenticator']['authMethods'] = [ - HttpBearerAuth::className(), - ]; - - return $behaviors; - } public function verbs(): array { diff --git a/frontend/modules/api/controllers/TemplateController.php b/frontend/modules/api/controllers/TemplateController.php index db782c1..4f622ef 100644 --- a/frontend/modules/api/controllers/TemplateController.php +++ b/frontend/modules/api/controllers/TemplateController.php @@ -5,22 +5,14 @@ namespace frontend\modules\api\controllers; use common\models\Document; use common\models\Template; use Yii; +use yii\filters\auth\CompositeAuth; use yii\filters\auth\HttpBearerAuth; +use yii\filters\ContentNegotiator; use yii\web\NotFoundHttpException; -use yii\rest\Controller; +use yii\web\Response; -class TemplateController extends Controller +class TemplateController extends ApiController { - public function behaviors(): array - { - $behaviors = parent::behaviors(); - - $behaviors['authenticator']['authMethods'] = [ - HttpBearerAuth::className(), - ]; - - return $behaviors; - } public function verbs(): array { @@ -34,7 +26,7 @@ class TemplateController extends Controller { $template = Template::find()->asArray()->all(); - if(empty($template)) { + if (empty($template)) { throw new NotFoundHttpException('Documents are not assigned'); } @@ -44,8 +36,7 @@ class TemplateController extends Controller public function actionGetTemplateFields(): array { $template_id = Yii::$app->request->get('template_id'); - if(empty($template_id) or !is_numeric($template_id)) - { + if (empty($template_id) or !is_numeric($template_id)) { throw new NotFoundHttpException('Incorrect template ID'); } @@ -55,7 +46,7 @@ class TemplateController extends Controller ->asArray() ->all(); - if(empty($templates)) { + if (empty($templates)) { throw new NotFoundHttpException('Documents are not assigned'); }