Skip to content
This repository was archived by the owner on Jan 17, 2022. It is now read-only.

Commit 0a4538d

Browse files
committed
Tweak reseorce keys and serializers
1 parent 7a3446b commit 0a4538d

File tree

4 files changed

+18
-46
lines changed

4 files changed

+18
-46
lines changed

Diff for: src/Generator/stubs/controller.stub

-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ use {{transformer.fullName}};
66

77
class {{controller.name}} extends Controller
88
{
9-
/**
10-
* Resource key.
11-
*
12-
* @var string
13-
*/
14-
protected $resourceKey = null;
15-
169
/**
1710
* Eloquent model.
1811
*

Diff for: src/Skeleton/BaseController.php

+18-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use League\Fractal\Pagination\Cursor;
1111
use League\Fractal\Resource\Collection;
1212
use League\Fractal\Resource\Item;
13+
use League\Fractal\Serializer\DataArraySerializer;
1314

1415
abstract class BaseController extends LaravelController
1516
{
@@ -71,11 +72,18 @@ abstract class BaseController extends LaravelController
7172
protected $maximumLimit = false;
7273

7374
/**
74-
* Resource key.
75+
* Resource key for an item.
7576
*
7677
* @var string
7778
*/
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';
7987

8088
/**
8189
* Constructor.
@@ -86,11 +94,12 @@ public function __construct(Request $request)
8694
{
8795
$this->model = $this->model();
8896
$this->transformer = $this->transformer();
89-
$this->fractal = new Manager();
90-
$this->request = $request;
9197

98+
$this->fractal = new Manager();
9299
$this->fractal->setSerializer($this->serializer());
93100

101+
$this->request = $request;
102+
94103
if ($this->request->has('include')) {
95104
$this->fractal->parseIncludes(camel_case($this->request->input('include')));
96105
}
@@ -117,7 +126,7 @@ abstract protected function transformer();
117126
*/
118127
protected function serializer()
119128
{
120-
return new Serializer();
129+
return new DataArraySerializer();
121130
}
122131

123132
/**
@@ -128,8 +137,6 @@ protected function serializer()
128137
*/
129138
public function index()
130139
{
131-
$this->resourceKey = empty($this->resourceKey) ? 'data' : str_plural($this->resourceKey);
132-
133140
$with = $this->getEagerLoad();
134141
$skip = (int) $this->request->input('skip', 0);
135142
$limit = $this->calculateLimit();
@@ -149,9 +156,7 @@ public function index()
149156
*/
150157
public function store()
151158
{
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);
155160

156161
if (!$data) {
157162
return $this->errorWrongArgs('Empty data');
@@ -199,9 +204,7 @@ public function show($id)
199204
*/
200205
public function update($id)
201206
{
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);
205208

206209
if (!$data) {
207210
return $this->errorWrongArgs('Empty data');
@@ -301,7 +304,7 @@ protected function setStatusCode($statusCode)
301304
*/
302305
protected function respondWithItem($item)
303306
{
304-
$resource = new Item($item, $this->transformer, $this->resourceKey);
307+
$resource = new Item($item, $this->transformer, $this->resourceKeySingular);
305308

306309
$rootScope = $this->prepareRootScope($resource);
307310

@@ -319,7 +322,7 @@ protected function respondWithItem($item)
319322
*/
320323
protected function respondWithCollection($collection, $skip = 0, $limit = 0)
321324
{
322-
$resource = new Collection($collection, $this->transformer, $this->resourceKey);
325+
$resource = new Collection($collection, $this->transformer, $this->resourceKeyPlural);
323326

324327
if ($limit) {
325328
$cursor = new Cursor($skip, $skip + $limit, $collection->count());

Diff for: src/Skeleton/Serializer.php

-24
This file was deleted.

Diff for: templates/Api/Serializers/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)