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.
abstract class BaseApiController extends BaseController
{
use AuthorizesRequests;
use DispatchesJobs;
use ValidatesRequests;
/**
* Return success response.
*
* @param mixed $result Data to be sent as response
* @param bool $wrapped States whether or not $result should be wrapped in a 'data' field
* If false it will be merged into the main object
* @return \Illuminate\Http\Response
*/
public function sendResponse($result = [], $wrapped = true)
{
if ($wrapped) {
$response = (object) [
'success' => true, 'data' => (object) $result,
];
} else {
if (! is_array($result)) {
if (
$result instanceof JsonResource
|| $result instanceof ResourceCollection
) {
$result = $result->toArray(request());
} elseif (method_exists($result, 'toArray')) {
$result = $result->toArray();
} else {
$result = (array) $result;
}
}
$response = (object) array_merge(
[
'success' => true,
], $result
);
}
return response()->json($response, 200);
}
/**
* Return error response.
*
* @return \Illuminate\Http\Response
*/
public function sendError($errorMessages = [], $code = 400)
{
$response = (object) [
'success' => false,
'messages' => $errorMessages,
];
return response()->json($response, $code);
}
protected function execute(Closure $closure, $transactional = false)
{
if ($transactional) {
return TransactionUtils::executeInTransaction(function () use ($closure) {
return $closure();
});
}
return $closure();
}
}Last updated