dennis-learning/symfony-response-bundle
Installation Add the bundle via Composer (though the package is named for Symfony, Laravel users can adapt the core concepts):
composer require dennis-learning/symfony-response-bundle
Note: Since this is a Symfony bundle, Laravel users will need to manually extract relevant logic (e.g., response formatting, HTTP status handling) into a Laravel-compatible package or service.
Key Classes to Explore
ResponseBuilder or ResponseFactory (if present) to understand how responses are structured.JsonResponse, ApiResponse).First Use Case
// app/Services/ApiResponseService.php
namespace App\Services;
use Illuminate\Http\JsonResponse;
class ApiResponseService {
public function success(array $data, int $status = 200): JsonResponse {
return response()->json([
'success' => true,
'data' => $data,
], $status);
}
}
use App\Services\ApiResponseService;
class UserController extends Controller {
public function show(ApiResponseService $response) {
return $response->success(['name' => 'John']);
}
}
Standardized API Responses
// Extend the service for nested data or errors
public function error(string $message, array $errors = [], int $status = 422): JsonResponse {
return response()->json([
'success' => false,
'message' => $message,
'errors' => $errors,
], $status);
}
HTTP Status Codes
201 for creation, 404 for not found).public function created(array $data): JsonResponse {
return response()->json($data, 201);
}
Middleware Integration
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Response;
class AddResponseMetadata {
public function handle(Response $response, Closure $next) {
$response->headers->set('X-Response-Time', microtime(true));
return $next($response);
}
}
Request-Response Pairing
use App\Http\Requests\StoreUserRequest;
class UserController {
public function store(StoreUserRequest $request, ApiResponseService $response) {
$validated = $request->validated();
return $response->success($validated);
}
}
ApiResponseService in PHPUnit:
$mock = $this->mock(ApiResponseService::class);
$mock->shouldReceive('success')->once()->andReturn(response()->json([]));
Symfony-Specific Dependencies
HttpFoundation). Laravel users must replace these with Laravel equivalents (e.g., Illuminate\Http\Response).interface ResponseInterface {
public function json(array $data, int $status);
}
Outdated Codebase
Response::create()).response()->json() instead of Symfony’s JsonResponse).Missing Documentation
Response class for patterns.dd() or dump() to inspect response structure:
dd($response->getContent());
$response->getStatusCode(); // Should match expectations (e.g., 200, 404).
Custom Response Classes
ApiResponseService for domain-specific responses:
class PaymentResponseService extends ApiResponseService {
public function paymentFailed(string $error): JsonResponse {
return $this->error($error, [], 402); // 402 = Payment Required
}
}
Dynamic Headers
$response = response()->json($data);
$response->header('Cache-Control', 'public, max-age=3600');
Localization
trans():
public function error(string $key, array $replace = []): JsonResponse {
return $this->error(trans($key, $replace), [], 422);
}
How can I help you explore Laravel packages today?