88use yii \base \Component ;
99use yii \helpers \ArrayHelper ;
1010use yii \helpers \Json ;
11+ use yii \helpers \Url ;
1112use yii \web \ErrorHandler ;
1213use yii \web \Response ;
1314use yii \web \ResponseFormatterInterface ;
@@ -52,11 +53,17 @@ class JsonApiResponseFormatter extends Component implements ResponseFormatterInt
5253 public function format ($ response )
5354 {
5455 $ response ->getHeaders ()->set ('Content-Type ' , 'application/vnd.api+json; charset=UTF-8 ' );
56+ $ apiDocument = [
57+ 'data ' => $ response ->data ,
58+ 'links ' => [
59+ 'self ' => Url::current ([], true )
60+ ]
61+ ];
62+ $ options = $ this ->encodeOptions ;
63+ if ($ this ->prettyPrint ) {
64+ $ options |= JSON_PRETTY_PRINT ;
65+ }
5566 if ($ response ->data !== null ) {
56- $ options = $ this ->encodeOptions ;
57- if ($ this ->prettyPrint ) {
58- $ options |= JSON_PRETTY_PRINT ;
59- }
6067 $ apiDocument = $ response ->data ;
6168 if ($ response ->isClientError || $ response ->isServerError ) {
6269 if (ArrayHelper::isAssociative ($ response ->data )) {
@@ -76,8 +83,7 @@ public function format($response)
7683 }
7784 $ apiDocument = ['errors ' => $ formattedErrors ];
7885 }
79-
80- $ response ->content = Json::encode ($ apiDocument , $ options );
8186 }
87+ $ response ->content = Json::encode ($ apiDocument , $ options );
8288 }
8389}
0 commit comments