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

Commit 9bed4cc

Browse files
committed
Service provider and work on controller
1 parent 8621a6b commit 9bed4cc

File tree

7 files changed

+102
-23
lines changed

7 files changed

+102
-23
lines changed

config/config.php

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php namespace App\Api\Controllers;
2+
3+
use App\User;
4+
use App\Api\Transformers\UserTransformer;
5+
6+
class UserController extends Controller
7+
{
8+
/**
9+
* Eloquent model.
10+
*
11+
* @return \Illuminate\Database\Eloquent\Model
12+
*/
13+
protected function model()
14+
{
15+
return new User;
16+
}
17+
18+
/**
19+
* Transformer for the current model.
20+
*
21+
* @return \League\Fractal\TransformerAbstract
22+
*/
23+
protected function transformer()
24+
{
25+
return new UserTransformer;
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php namespace App\Api\Transformers;
2+
3+
use App\User;
4+
use League\Fractal\TransformerAbstract;
5+
6+
class UserTransformer extends TransformerAbstract
7+
{
8+
/**
9+
* Turn this item object into a generic array
10+
* @param $item
11+
* @return array
12+
*/
13+
public function transform(User $item)
14+
{
15+
return [
16+
'id' => (int)$item->id,
17+
'email' => (string)$item->email,
18+
'created_at' => (string)$item->created_at,
19+
'updated_at' => (string)$item->updated_at,
20+
];
21+
}
22+
23+
}

ServiceProvider.php renamed to src/ServiceProvider.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,19 @@ public function register()
2222
// $this->app->singleton('command.api.make', function ($app) {
2323
// return new ApiMakeCommand($app['files']);
2424
// });
25-
26-
$this->commands('command.api.make');
25+
//
26+
// $this->commands('command.api.make');
2727
}
2828

2929
/**
3030
* Bootstrap the application events.
31-
*
32-
* @return void
3331
*/
3432
public function boot()
3533
{
3634
$this->publishes([
3735
__DIR__.'/config/config.php' => config_path('laravel-api-generator.php'),
3836
]);
3937

40-
include app_path(config('routes_file'));
38+
require base_path(config('laravel-api-generator.routes_file'));
4139
}
4240
}

src/Skeleton/BaseController.php

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,41 @@ abstract class BaseController extends LaravelController
4141
protected $transformer;
4242

4343
/**
44-
* Create fractal manager instance.
45-
* Called in child constructor.
44+
* Do we need to unguard the model before create/update?
45+
*
46+
* @var boolean
47+
*/
48+
protected $unguard = false;
49+
50+
/**
51+
* Constructor.
4652
*/
47-
protected function prepareFractalAndValidator()
53+
public function __construct()
4854
{
55+
$this->model = $this->model();
56+
$this->transformer = $this->transformer();
4957
$this->fractal = new Manager;
58+
5059
if (Input::has('include'))
5160
{
5261
$this->fractal->parseIncludes(camel_case(Input::get('include')));
5362
}
5463
}
5564

65+
/**
66+
* Eloquent model.
67+
*
68+
* @return \Illuminate\Database\Eloquent\Model
69+
*/
70+
abstract protected function model();
71+
72+
/**
73+
* Transformer for the current model.
74+
*
75+
* @return \League\Fractal\TransformerAbstract
76+
*/
77+
abstract protected function transformer();
78+
5679
/**
5780
* Getter for statusCode.
5881
*
@@ -283,13 +306,9 @@ public function store()
283306
return $this->errorWrongArgs('Validation failed');
284307
}
285308

286-
$this->model->unguard();
309+
$this->unguardIfNeeded();
287310

288311
$item = $this->model->create($data);
289-
if (!$item)
290-
{
291-
return $this->errorNotFound();
292-
}
293312

294313
return $this->respondWithItem($item, $this->transformer);
295314
}
@@ -307,7 +326,6 @@ public function show($id)
307326
$with = $this->getEagerLoad();
308327

309328
$item = $this->findItem($id, $with);
310-
311329
if (!$item)
312330
{
313331
return $this->errorNotFound();
@@ -334,9 +352,7 @@ public function update($id)
334352
return $this->errorWrongArgs('Empty data');
335353
}
336354

337-
338355
$item = $this->findItem($id);
339-
340356
if (!$item)
341357
{
342358
return $this->errorNotFound();
@@ -348,7 +364,8 @@ public function update($id)
348364
return $this->errorWrongArgs('Validation failed');
349365
}
350366

351-
$this->model->unguard();
367+
$this->unguardIfNeeded();
368+
352369
$item->fill($data);
353370
$item->save();
354371

@@ -413,7 +430,7 @@ protected function getEagerLoad()
413430
}
414431

415432
/**
416-
* Get the item according to mode.
433+
* Get item according to mode.
417434
*
418435
* @param $id
419436
* @param array $with
@@ -430,4 +447,14 @@ protected function findItem($id, array $with = [])
430447
return $this->model->with($with)->find($id);
431448
}
432449

450+
/**
451+
* Unguard eloquent model if needed.
452+
*/
453+
protected function unguardIfNeeded()
454+
{
455+
if ($this->unguard)
456+
{
457+
$this->model->unguard();
458+
}
459+
}
433460
}

src/config/config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
'routes_file' => 'app/Api/routes.php',
5+
'controllers_path' => 'app/Api/Controllers',
6+
'transformers_path' => 'app/Api/Transformers',
7+
'controller_stub' => 'vendor/arrilot/laravel-api-generator/src/Generator/controller.stub',
8+
'transformer_stub' => 'vendor/arrilot/laravel-api-generator/src/Generator/transformer.stub'
9+
];

templates/Api/Transformers/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)