deozza/response-maker-bundle
Installation
composer require deozza/response-maker-bundle
Ensure the package is registered in config/app.php under providers:
Deozza\ResponseMakerBundle\ResponseMakerServiceProvider::class,
Basic Usage
Inject the ResponseMaker service into your controller or service:
use Deozza\ResponseMakerBundle\ResponseMaker;
class ExampleController extends Controller
{
protected $responseMaker;
public function __construct(ResponseMaker $responseMaker)
{
$this->responseMaker = $responseMaker;
}
public function index()
{
$data = ['message' => 'Hello, World!'];
return $this->responseMaker->make($data);
}
}
First Use Case
Replace manual JSON responses with a standardized approach:
// Before
return response()->json(['success' => true]);
// After
return $this->responseMaker->make(['success' => true]);
Consistent JSON Responses
Use make() for all API responses to enforce a uniform structure:
$response = $this->responseMaker->make([
'data' => $user,
'meta' => ['status' => 'success']
]);
HTTP Status Codes Pass a status code as the second argument:
return $this->responseMaker->make(['error' => 'Not Found'], 404);
Error Handling Standardize error responses:
try {
$data = $this->fetchData();
} catch (\Exception $e) {
return $this->responseMaker->make(['error' => $e->getMessage()], 500);
}
API Resources
Combine with Laravel’s ApiResource for structured payloads:
return $this->responseMaker->make(new UserResource($user));
Form Requests
Validate and respond using ResponseMaker:
public function store(StoreUserRequest $request)
{
$validated = $request->validated();
return $this->responseMaker->make($validated);
}
Middleware Responses Use in middleware for unauthorized access:
return $this->responseMaker->make(['error' => 'Unauthorized'], 401);
Custom Response Classes
Extend the ResponseMaker to add domain-specific logic:
class ApiResponseMaker extends ResponseMaker
{
public function success($data, $message = 'Success')
{
return $this->make([
'success' => true,
'message' => $message,
'data' => $data
]);
}
}
Response Wrapping
Automatically wrap responses in a meta layer:
$this->responseMaker->wrap($data, ['status' => 'ok']);
No Built-in Validation
The package does not validate input data. Ensure data is sanitized before passing to make():
// Avoid:
$this->responseMaker->make($_GET); // Unsafe!
// Do:
$this->responseMaker->make(array_filter($_GET));
Overriding Default Behavior
If extending ResponseMaker, ensure parent methods are called:
public function make($data, $status = 200, array $headers = [])
{
$data = $this->customTransform($data); // Add logic here
return parent::make($data, $status, $headers);
}
Performance with Large Data
Avoid passing large arrays/objects directly. Use json_encode options or chunk data:
$this->responseMaker->make($data, 200, [], JSON_PRETTY_PRINT);
Check Headers
If responses lack Content-Type: application/json, verify no middleware is overriding headers:
$this->responseMaker->make($data, 200, ['Content-Type' => 'application/json']);
Logging Responses Log responses for debugging:
\Log::debug('Response:', [
'data' => $data,
'status' => $status,
'headers' => $headers
]);
Custom Serializers
Override the serialize() method to support custom data types (e.g., DateTime):
public function serialize($data)
{
if ($data instanceof \DateTime) {
return $data->format('Y-m-d H:i:s');
}
return parent::serialize($data);
}
Response Hooks Add hooks for pre/post-processing:
$this->responseMaker->onMake(function ($data, $status) {
$data['timestamp'] = now()->toIso8601String();
return $data;
});
Testing
Mock ResponseMaker in tests:
$mock = Mockery::mock(ResponseMaker::class);
$mock->shouldReceive('make')->andReturn(response()->json(['mocked' => true]));
How can I help you explore Laravel packages today?