8
8
use yii \base \Component ;
9
9
use yii \helpers \ArrayHelper ;
10
10
use yii \helpers \Json ;
11
+ use yii \helpers \Url ;
11
12
use yii \web \ErrorHandler ;
12
13
use yii \web \Response ;
13
14
use yii \web \ResponseFormatterInterface ;
@@ -52,11 +53,17 @@ class JsonApiResponseFormatter extends Component implements ResponseFormatterInt
52
53
public function format ($ response )
53
54
{
54
55
$ 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
+ }
55
66
if ($ response ->data !== null ) {
56
- $ options = $ this ->encodeOptions ;
57
- if ($ this ->prettyPrint ) {
58
- $ options |= JSON_PRETTY_PRINT ;
59
- }
60
67
$ apiDocument = $ response ->data ;
61
68
if ($ response ->isClientError || $ response ->isServerError ) {
62
69
if (ArrayHelper::isAssociative ($ response ->data )) {
@@ -76,8 +83,7 @@ public function format($response)
76
83
}
77
84
$ apiDocument = ['errors ' => $ formattedErrors ];
78
85
}
79
-
80
- $ response ->content = Json::encode ($ apiDocument , $ options );
81
86
}
87
+ $ response ->content = Json::encode ($ apiDocument , $ options );
82
88
}
83
89
}
0 commit comments