10
10
use League \Fractal \Pagination \Cursor ;
11
11
use League \Fractal \Resource \Collection ;
12
12
use League \Fractal \Resource \Item ;
13
+ use League \Fractal \Serializer \DataArraySerializer ;
13
14
14
15
abstract class BaseController extends LaravelController
15
16
{
@@ -71,11 +72,18 @@ abstract class BaseController extends LaravelController
71
72
protected $ maximumLimit = false ;
72
73
73
74
/**
74
- * Resource key.
75
+ * Resource key for an item .
75
76
*
76
77
* @var string
77
78
*/
78
- protected $ resourceKey = null ;
79
+ protected $ resourceKeySingular = 'data ' ;
80
+
81
+ /**
82
+ * Resource key for a collection.
83
+ *
84
+ * @var string
85
+ */
86
+ protected $ resourceKeyPlural = 'data ' ;
79
87
80
88
/**
81
89
* Constructor.
@@ -86,11 +94,12 @@ public function __construct(Request $request)
86
94
{
87
95
$ this ->model = $ this ->model ();
88
96
$ this ->transformer = $ this ->transformer ();
89
- $ this ->fractal = new Manager ();
90
- $ this ->request = $ request ;
91
97
98
+ $ this ->fractal = new Manager ();
92
99
$ this ->fractal ->setSerializer ($ this ->serializer ());
93
100
101
+ $ this ->request = $ request ;
102
+
94
103
if ($ this ->request ->has ('include ' )) {
95
104
$ this ->fractal ->parseIncludes (camel_case ($ this ->request ->input ('include ' )));
96
105
}
@@ -117,7 +126,7 @@ abstract protected function transformer();
117
126
*/
118
127
protected function serializer ()
119
128
{
120
- return new Serializer ();
129
+ return new DataArraySerializer ();
121
130
}
122
131
123
132
/**
@@ -128,8 +137,6 @@ protected function serializer()
128
137
*/
129
138
public function index ()
130
139
{
131
- $ this ->resourceKey = empty ($ this ->resourceKey ) ? 'data ' : str_plural ($ this ->resourceKey );
132
-
133
140
$ with = $ this ->getEagerLoad ();
134
141
$ skip = (int ) $ this ->request ->input ('skip ' , 0 );
135
142
$ limit = $ this ->calculateLimit ();
@@ -149,9 +156,7 @@ public function index()
149
156
*/
150
157
public function store ()
151
158
{
152
- $ key = empty ($ this ->resourceKey ) ? 'data ' : str_singular ($ this ->resourceKey );
153
-
154
- $ data = $ this ->request ->json ()->get ($ key );
159
+ $ data = $ this ->request ->json ()->get ($ this ->resourceKeySingular );
155
160
156
161
if (!$ data ) {
157
162
return $ this ->errorWrongArgs ('Empty data ' );
@@ -199,9 +204,7 @@ public function show($id)
199
204
*/
200
205
public function update ($ id )
201
206
{
202
- $ key = empty ($ this ->resourceKey ) ? 'data ' : str_singular ($ this ->resourceKey );
203
-
204
- $ data = $ this ->request ->json ()->get ($ key );
207
+ $ data = $ this ->request ->json ()->get ($ this ->resourceKeySingular );
205
208
206
209
if (!$ data ) {
207
210
return $ this ->errorWrongArgs ('Empty data ' );
@@ -301,7 +304,7 @@ protected function setStatusCode($statusCode)
301
304
*/
302
305
protected function respondWithItem ($ item )
303
306
{
304
- $ resource = new Item ($ item , $ this ->transformer , $ this ->resourceKey );
307
+ $ resource = new Item ($ item , $ this ->transformer , $ this ->resourceKeySingular );
305
308
306
309
$ rootScope = $ this ->prepareRootScope ($ resource );
307
310
@@ -319,7 +322,7 @@ protected function respondWithItem($item)
319
322
*/
320
323
protected function respondWithCollection ($ collection , $ skip = 0 , $ limit = 0 )
321
324
{
322
- $ resource = new Collection ($ collection , $ this ->transformer , $ this ->resourceKey );
325
+ $ resource = new Collection ($ collection , $ this ->transformer , $ this ->resourceKeyPlural );
323
326
324
327
if ($ limit ) {
325
328
$ cursor = new Cursor ($ skip , $ skip + $ limit , $ collection ->count ());
0 commit comments