From 5737015bebd9a26149f420ac07bc0f855cf88724 Mon Sep 17 00:00:00 2001 From: liufee Date: Sun, 16 Aug 2020 22:28:43 +0800 Subject: [PATCH] fix:response format --- api/behaviors/ResponseFormatBehavior.php | 67 ++++++++++++ api/config/main.php | 5 +- backend/config/main.php | 30 ++++-- common/behaviors/ResponseFormatBehavior.php | 109 -------------------- common/config/main.php | 1 + common/models/Article.php | 3 +- frontend/config/main.php | 22 ++-- frontend/controllers/ArticleController.php | 12 +++ 8 files changed, 123 insertions(+), 126 deletions(-) create mode 100644 api/behaviors/ResponseFormatBehavior.php delete mode 100644 common/behaviors/ResponseFormatBehavior.php diff --git a/api/behaviors/ResponseFormatBehavior.php b/api/behaviors/ResponseFormatBehavior.php new file mode 100644 index 00000000..fc7f89fa --- /dev/null +++ b/api/behaviors/ResponseFormatBehavior.php @@ -0,0 +1,67 @@ + [$this, 'beforeSend'], + ]; + } + + public function beforeSend() + { + $this->_response = Yii::$app->getResponse(); + + if( !$this->negotiate ){ + $this->_response->format = $this->format; + }else { + $this->negotiate(); + } + + } + + private function negotiate() + { + $acceptTypes = Yii::$app->getRequest()->getAcceptableContentTypes(); + $acceptTypes = array_keys($acceptTypes); + foreach ($acceptTypes as $acceptType){ + switch ($acceptType) { + case "text/plain": + $this->_response->format = Response::FORMAT_RAW; + break; + case "application/html": + case "text/html": + case "*/*": + $this->_response->format = $this->format; + break; + case "application/json": + case "text/json": + $this->_response->format = Response::FORMAT_JSON; + break; + case "application/xml": + case "text/xml": + $this->_response->format = Response::FORMAT_XML; + break; + } + } + } +} \ No newline at end of file diff --git a/api/config/main.php b/api/config/main.php index 02f05ede..0443a0df 100644 --- a/api/config/main.php +++ b/api/config/main.php @@ -82,10 +82,9 @@ 'enableCookieValidation' => false, ], 'response' => [ + 'format' => null, 'as format' => [ - 'class' => common\behaviors\ResponseFormatBehavior::className(), - 'isApi' => true,//this options makes application/html and text/html return defaultResponseFormat - 'defaultResponseFormat' => yii\web\Response::FORMAT_JSON, + 'class' => api\behaviors\ResponseFormatBehavior::className(), ] ], ], diff --git a/backend/config/main.php b/backend/config/main.php index d9e61aa8..754c445c 100644 --- a/backend/config/main.php +++ b/backend/config/main.php @@ -70,12 +70,6 @@ 'showScriptName' => true,//隐藏index.php 'enableStrictParsing' => false, ], - 'response' => [ - 'as format' => [ - 'class' => common\behaviors\ResponseFormatBehavior::className(), - 'defaultAjaxResponseFormat' => yii\web\Response::FORMAT_JSON,//if http Accept header is "application/html" - ] - ], 'i18n' => [ 'translations' => [//多语言包设置 'app*' => [ @@ -101,6 +95,30 @@ 'assetManager' => [ 'linkAssets' => false,//若为unix like系统这里可以修改成true则创建css js文件软链接到assets而不是拷贝css js到assets目录 'bundles' => [ + yii\widgets\ActiveFormAsset::className() => [ + 'js' => [ + ] + ], + yii\web\JqueryAsset::className() => [ + 'js' => [ + ], + ], + yii\web\YiiAsset::className() => [ + 'js' => [ + ], + ], + yii\validators\ValidationAsset::className() => [ + 'js' => [ + ] + ], + yii\grid\GridViewAsset::className() => [ + 'js' => [ + ] + ], + yii\widgets\PjaxAsset::className() => [ + 'js' => [ + ] + ], backend\assets\AppAsset::className() => [ 'sourcePath' => '@backend/web/static', 'css' => [ diff --git a/common/behaviors/ResponseFormatBehavior.php b/common/behaviors/ResponseFormatBehavior.php deleted file mode 100644 index ea12f11a..00000000 --- a/common/behaviors/ResponseFormatBehavior.php +++ /dev/null @@ -1,109 +0,0 @@ - [$this, 'beforeSend'], - ]; - } - - public function beforeSend() - { - if( !$this->autoNegotiate ){ - $this->force(); - }else { - $this->negotiate(); - } - - } - - private function force() - { - /** @var Response $response */ - $response = $this->owner; - if( Yii::$app->getRequest()->getIsAjax() ){ - $response->format = $this->ajaxResponseFormat; - }else{ - $response->format = $this->responseFormat; - } - } - - private function negotiate() - { - /** @var Response $response */ - $response = $this->owner; - - $acceptTypes = Yii::$app->getRequest()->getAcceptableContentTypes(); - $acceptTypes = array_keys($acceptTypes); - $found = false; - foreach ($acceptTypes as $acceptType){ - switch ($acceptType) { - case "text/plain": - $response->format = Response::FORMAT_RAW; - $found = true; - break; - case "application/html": - case "text/html": - if( $this->isApi ){//api default response format(makes web browsers request api get a correct response format) - $response->format = $this->defaultResponseFormat; - }else if( Yii::$app->getRequest()->getIsAjax() && is_array(Yii::$app->getResponse()->content) && Yii::$app->getResponse()->format === $this->defaultResponseFormat ){ - //(backend,frontend) ajax if returns an array and not set response format, will use the defaultAjaxResponseFormat - $response->format = $this->defaultAjaxResponseFormat; - }else{ - $response->format = Response::FORMAT_HTML; - } - $found = true; - break; - case "application/json": - case "text/json": - $response->format = Response::FORMAT_JSON; - $found = true; - break; - case "application/xml": - case "text/xml": - $response->format = Response::FORMAT_XML; - $found = true; - break; - } - if( $found ){ - break; - } - } - - if( $found === false ){ - if (Yii::$app->getRequest()->getIsAjax()) { - $response->format = $this->defaultAjaxResponseFormat; - }else{ - $response->format = $this->defaultResponseFormat; - } - } - - $exception = Yii::$app->getErrorHandler()->exception; - if ( YII_DEBUG && $exception !== null && !is_array($response->data)) {//debug env and shows error page - $response->format = Response::FORMAT_HTML; - } - } -} \ No newline at end of file diff --git a/common/config/main.php b/common/config/main.php index 4983db0a..e7508624 100644 --- a/common/config/main.php +++ b/common/config/main.php @@ -34,6 +34,7 @@ ], 'formatter' => [//global display format configuration 'dateFormat' => 'php:Y-m-d H:i', + 'datetimeFormat' => 'php:Y-m-d H:i:s', 'decimalSeparator' => ',', 'thousandSeparator' => ' ', 'currencyCode' => 'CHY', diff --git a/common/models/Article.php b/common/models/Article.php index 01b9ca7c..778a9dfb 100644 --- a/common/models/Article.php +++ b/common/models/Article.php @@ -106,7 +106,8 @@ public function rules() [['cid', 'type', 'status', 'sort', 'author_id', 'can_comment', 'visibility'], 'integer'], [['cid', 'sort', 'author_id'], 'compare', 'compareValue' => 0, 'operator' => '>='], [['title', 'status'], 'required'], - [['can_comment', 'visibility'], 'default', 'value' => Constants::YesNo_Yes], + [['can_comment'], 'default', 'value' => Constants::YesNo_Yes], + [['visibility'], 'default', 'value' => Constants::ARTICLE_VISIBILITY_PUBLIC], [['thumb'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif, webp'], [['images'], 'safe'], [['created_at', 'updated_at'], 'safe'], diff --git a/frontend/config/main.php b/frontend/config/main.php index c3e46073..895b48d4 100644 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -78,12 +78,6 @@ 'list/' => 'site/index', ], ], - 'response' => [ - 'as format' => [ - 'class' => common\behaviors\ResponseFormatBehavior::className(), - 'defaultAjaxResponseFormat' => yii\web\Response::FORMAT_JSON,//if http Accept header is "application/html" - ] - ], 'i18n' => [ 'translations' => [ 'app*' => [ @@ -111,8 +105,22 @@ 'assetManager' => [ 'linkAssets' => false, 'bundles' => [ + yii\widgets\ActiveFormAsset::className() => [ + 'js' => [ + ] + ], yii\web\JqueryAsset::className() => [ - 'js' => [], + 'js' => [ + ], + ], + yii\web\YiiAsset::className() => [ + 'js' => [ + + ], + ], + yii\validators\ValidationAsset::className() => [ + 'js' => [ + ] ], frontend\assets\AppAsset::className() => [ 'sourcePath' => '@frontend/web/static', diff --git a/frontend/controllers/ArticleController.php b/frontend/controllers/ArticleController.php index 17359ff4..ce2c1387 100644 --- a/frontend/controllers/ArticleController.php +++ b/frontend/controllers/ArticleController.php @@ -192,6 +192,7 @@ public function actionView($id) */ public function actionViewAjax($id) { + Yii::$app->getResponse()->format = Response::FORMAT_JSON; /** @var ArticleServiceInterface $articleService */ $articleService = Yii::$app->get(ArticleServiceInterface::ServiceName); $model = $articleService->getArticleById($id); @@ -244,9 +245,20 @@ public function actionComment() /** * @param $id * @return string|\yii\web\Response + * @throws \yii\base\InvalidConfigException + * @throws NotFoundHttpException */ public function actionPassword($id) { + /** @var ArticleServiceInterface $articleService */ + $articleService = Yii::$app->get(ArticleServiceInterface::ServiceName); + $article = $articleService->getArticleById($id); + if( $article === null ) { + throw new NotFoundHttpException(Yii::t("frontend", "Article id {id} is not exists", ['id' => $id])); + } + if( $article->visibility !== Constants::ARTICLE_VISIBILITY_SECRET ){ + return $this->redirect(Url::to(['article/view', 'id'=>$id])); + } $model = new ArticlePasswordForm(); if ($model->load(Yii::$app->getRequest()->post()) && $model->checkPassword($id)) {