Skip to content

Instantly share code, notes, and snippets.

@zulfikar-ditya
Last active April 4, 2023 04:42
Show Gist options
  • Save zulfikar-ditya/31eb9845e84e1de4d89a4888391d657a to your computer and use it in GitHub Desktop.
Save zulfikar-ditya/31eb9845e84e1de4d89a4888391d657a to your computer and use it in GitHub Desktop.
This is my controller api helper for laravel app
<?php
namespace App\Helpers;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
trait ControllerHelpers
{
/**
* Response json
*/
public function responseJson(mixed $arr, int $code = 200): JsonResponse
{
return response()->json($arr, $code);
}
/**
* Response json validation error
*/
public function responseJsonValidate(\Illuminate\Support\MessageBag $error, int $code = 422): JsonResponse
{
return response()->json(compact('error'), $code);
}
/**
* Response json message
*/
public function responseJsonMessage(string $message, int $code = 200): JsonResponse
{
return response()->json(compact('message'), $code);
}
/**
* Response json data
*/
public function responseJsonData(mixed $data, string $message = 'success get data', int $code = 200): JsonResponse
{
return response()->json(compact('data', 'message'), $code);
}
/**
* Response json message crud
*/
public function responseJsonMessageCrud(bool $success = true, string $method = 'create', string $message = null, string $exception_message = null, int $code = 200, mixed $data = null): JsonResponse
{
if ($success) {
$final_message = 'Success ';
} else {
$final_message = 'Failed ';
}
if ($method == 'create') {
$final_message .= 'insert new data. ';
} else if ($method == 'edit') {
$final_message .= 'update data. ';
} else if ($method == 'delete') {
$final_message .= 'delete data, ';
}
if ($message != null) {
$final_message .= $message . ' ';
}
if ($exception_message != null) {
$final_message .= $exception_message;
}
if ($data == null) {
return response()->json(['message' => $final_message], $code);
} else {
return response()->json(['message' => $final_message, "result" => $data], $code);
}
}
/**
* Response json message crud
*/
public function responseMessageCrud(bool $success = true, string $method = 'create', string $message = null, string $exception_message = null): array
{
if ($success) {
$final_message = 'Success ';
} else {
$final_message = 'Failed ';
}
if ($method == 'create') {
$final_message .= 'insert new data. ';
} else if ($method == 'edit') {
$final_message .= 'update data. ';
} else if ($method == 'delete') {
$final_message .= 'delete data, ';
}
if ($message != null) {
$final_message .= $message . ' ';
}
if ($exception_message != null) {
$final_message .= $exception_message;
}
return [
'success' => $success,
'message' => $final_message
];
}
/**
* Response file
*/
public function responseFile(string $file_name): \Symfony\Component\HttpFoundation\BinaryFileResponse
{
return response()->file(Storage::url($file_name));
}
/**
* Response download from storage
*/
public function responseDownloadStorage(string $file): \Symfony\Component\HttpFoundation\BinaryFileResponse
{
return response()->download(storage_path('/app/public/' . $file));
}
/**
* Response download
*/
public function responseDownload(string $file): \Symfony\Component\HttpFoundation\BinaryFileResponse
{
return response()->download($file);
}
/**
* Upload file to storage
*/
public function uploadFile(\Illuminate\Http\UploadedFile $file, string $folder = 'unknown'): string|bool
{
return Storage::disk('public')->put($folder, $file);
}
/**
* Delete file from storage
*/
public function deleteFile(string $file_path): bool
{
return Storage::disk('public')->delete($file_path);
}
/**
* Validate api
*/
public function validateApi($request, $rules): bool|JsonResponse
{
$validate = Validator::make($request, $rules);
if ($validate->fails()) {
return $this->responseJsonValidate($validate->errors());
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment