Controllers
Creation of a controller
This is an example of a controller in Balance and, as mentioned above, it will call the services' methods that are needed for the execution of the needed operation.
A controller can only call one or more services, it can never call another controller (other than its parent) or repositories.
If the logic of the operation includes changes to the database a transaction must be initiated by passing true as the second argument of the execute method.
For more info always check the official Laravel documentation.
No businedd logic must be present inside the controller, such operations must always be contained and executed in the services.
class ThreadsApiController extends BaseApiController
{
private ThreadServiceInterface $threadService;
public function __construct(ThreadServiceInterface $threadService)
{
$this->threadService = $threadService;
}
public function index()
{
abort_if(Gate::denies('thread_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(function () {
return $this->sendResponse(
new BasePaginatedCollectionResource($this->threadService->index(), ThreadResource::class)
);
});
}
public function create()
{
abort_if(Gate::denies('thread_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(function () {
return $this->sendResponse(
[],
false
);
});
}
public function store(StoreThreadRequest $request)
{
abort_if(Gate::denies('thread_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(
function () use ($request) {
return $this->sendResponse(
new ThreadResource($this->threadService->store(
$request->input('name'),
$request->input('status'),
$request->input('description'),
$request->input('color'),
$request->input('channel_id')
))
);
},
true
);
}
public function edit(Thread $thread)
{
abort_if(Gate::denies('thread_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(function () use ($thread) {
return $this->sendResponse(
new ThreadResource($this->threadService->edit($thread))
);
});
}
public function show(Thread $thread)
{
abort_if(Gate::denies('thread_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(function () use ($thread) {
return $this->sendResponse(
new ThreadResource($this->threadService->show($thread))
);
});
}
public function update(UpdateThreadRequest $request, Thread $thread)
{
abort_if(Gate::denies('thread_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(
function () use ($request, $thread) {
return $this->sendResponse(
new ThreadResource($this->threadService->update(
$thread,
$request->input('name'),
$request->input('status'),
$request->input('description'),
$request->input('color'),
$request->input('channel_id')
))
);
},
true
);
}
public function destroy(Thread $thread)
{
abort_if(Gate::denies('thread_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return $this->execute(function () use ($thread) {
$this->threadService->destroy($thread);
return $this->sendResponse(
[],
false
);
}, true);
}
}Each controller must extend the BaseApiController class, which contains common methods.
Last updated