feat: Initial Laravel API-only starter kit
- Laravel 12 with Sanctum authentication - API versioning with grazulex/laravel-apiroute - spatie/laravel-query-builder for filtering/sorting - spatie/laravel-data for DTOs - dedoc/scramble for auto API documentation - Pest PHP testing framework - Docker development environment - Standardized JSON API responses - Rate limiting and CORS configuration - Comprehensive README documentation
This commit is contained in:
70
app/Traits/ApiResponse.php
Normal file
70
app/Traits/ApiResponse.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
trait ApiResponse
|
||||
{
|
||||
protected function success(
|
||||
mixed $data = null,
|
||||
string $message = 'Success',
|
||||
int $code = Response::HTTP_OK
|
||||
): JsonResponse {
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => $message,
|
||||
'data' => $data,
|
||||
], $code);
|
||||
}
|
||||
|
||||
protected function created(
|
||||
mixed $data = null,
|
||||
string $message = 'Resource created successfully'
|
||||
): JsonResponse {
|
||||
return $this->success($data, $message, Response::HTTP_CREATED);
|
||||
}
|
||||
|
||||
protected function noContent(): JsonResponse
|
||||
{
|
||||
return response()->json(null, Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
||||
protected function error(
|
||||
string $message = 'Error',
|
||||
int $code = Response::HTTP_BAD_REQUEST,
|
||||
array $errors = []
|
||||
): JsonResponse {
|
||||
$response = [
|
||||
'success' => false,
|
||||
'message' => $message,
|
||||
];
|
||||
|
||||
if (! empty($errors)) {
|
||||
$response['errors'] = $errors;
|
||||
}
|
||||
|
||||
return response()->json($response, $code);
|
||||
}
|
||||
|
||||
protected function notFound(string $message = 'Resource not found'): JsonResponse
|
||||
{
|
||||
return $this->error($message, Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
protected function unauthorized(string $message = 'Unauthorized'): JsonResponse
|
||||
{
|
||||
return $this->error($message, Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
protected function forbidden(string $message = 'Forbidden'): JsonResponse
|
||||
{
|
||||
return $this->error($message, Response::HTTP_FORBIDDEN);
|
||||
}
|
||||
|
||||
protected function validationError(array $errors, string $message = 'Validation failed'): JsonResponse
|
||||
{
|
||||
return $this->error($message, Response::HTTP_UNPROCESSABLE_ENTITY, $errors);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user