Skip to content

Commit

Permalink
fix:response format
Browse files Browse the repository at this point in the history
  • Loading branch information
liufee committed Aug 16, 2020
1 parent e7bb37a commit 5737015
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 126 deletions.
67 changes: 67 additions & 0 deletions api/behaviors/ResponseFormatBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Author: lf
* Blog: https://blog.feehi.com
* Email: [email protected]
* Created at: 2018-08-15 23:30
*/
namespace api\behaviors;

use Yii;
use yii\web\Response;

class ResponseFormatBehavior extends \yii\base\Behavior
{

public $negotiate = true;

public $format = Response::FORMAT_JSON;

/** @var Response $response */
private $_response;

public function events()
{
return [
Response::EVENT_BEFORE_SEND => [$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;
}
}
}
}
5 changes: 2 additions & 3 deletions api/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]
],
],
Expand Down
30 changes: 24 additions & 6 deletions backend/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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*' => [
Expand All @@ -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' => [
Expand Down
109 changes: 0 additions & 109 deletions common/behaviors/ResponseFormatBehavior.php

This file was deleted.

1 change: 1 addition & 0 deletions common/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion common/models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
22 changes: 15 additions & 7 deletions frontend/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@
'list/<page:\d+>' => '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*' => [
Expand Down Expand Up @@ -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',
Expand Down
12 changes: 12 additions & 0 deletions frontend/controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 5737015

Please sign in to comment.