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:
Jean-Marc Strauven
2025-12-25 06:33:21 +01:00
commit 5900990527
70 changed files with 13696 additions and 0 deletions

View 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);
}
}