patternphplaravelMinor
Laravel API design
Viewed 0 times
designapilaravel
Problem
I've been designing and coding my Laravel API boilerplate for couple days now, and I'd like to hear some advice/improvement hints!
I'm pretty satisfied with the result, but I'm also aware, there might be (and surely are) some things that can be improved.
Basic concept:
Every controller should extend my
I'm pretty satisfied with the result, but I'm also aware, there might be (and surely are) some things that can be improved.
Basic concept:
Every controller should extend my
App\Http\Controllers\ApiController (actually, I'm thinking about moving this controller to App\Support\Http\Controllers\ApiController):repository()->all();
$transformed = $this->transformer()->collection($instances);
return response()->ok($transformed);
}
public function show($id)
{
$instance = $this->repository()->show($id);
$transformed = $this->transformer()->item($instance);
return response()->ok($transformed);
}
public function store(Request $request)
{
$this->validateStoreRequest();
$this->repository()->store($request->input());
return response()->created();
}
public function update(Request $request, $id)
{
$this->validateStoreRequest();
$this->repository()->update($id, $request->input());
return response()->updated();
}
public function destroy($id)
{
$this->repository()->destroy($id);
return response()->destroyed();
}
private function validateStoreRequest()
{
app(
static::$storeRequest ?: app('naming')->parse(static::class)->storeRequest()
);
}
private function validateUpdateRequest()
{
app(
static::$storeRequest ?: app('naming')->parse(static::class)->updateRequest()
);
}
private function transformer()
{
return app(
static::$transformer ?: app('naming')->parse(static::class)->transformer()
);
}
private function repository()
{
return app(
static::$repository ?: app('naming')->parse(static::class)->repository()
);
}
}Solution
- Why $repository and $transformer are static?
- Seems like $storeRequest and $updateRequest are just validators - do you really need to have them static?
- No type hinting for methods of
abstract class Transformer?
Context
StackExchange Code Review Q#105756, answer score: 2
Revisions (0)
No revisions yet.