Installation
composer require dbstudios/doze-bundle
Register the Bundle
Add to app/AppKernel.php:
new DaybreakStudios\DozeBundle\DaybreakStudiosDozeBundle(),
Configure Services
Define services.yml (Symfony 2.x) or config/services.yaml (Symfony 3.x+):
services:
app.doze.responder:
class: DaybreakStudios\DozeBundle\ResponderService
arguments: ['@serializer', '@request_stack']
First Use Case
Inject ResponderService into a controller and use it to return standardized API responses:
use DaybreakStudios\DozeBundle\ResponderService;
class ApiController extends AbstractController
{
public function index(ResponderService $responder)
{
$data = ['message' => 'Hello, API!'];
return $responder->respond($data);
}
}
Consistent Structure: Use ResponderService to enforce a uniform response format (e.g., data, meta, errors).
return $responder->respond($data, 200, ['X-RateLimit-Limit' => 100]);
Error Handling: Leverage built-in error responses:
return $responder->error('User not found', 404);
Custom Normalizers: Extend serialization for complex objects (e.g., Doctrine entities):
app.serializer.normalizer.entity:
class: App\Serializer\EntityNormalizer
arguments: ['@doctrine.orm.entity_manager']
Pagination: Normalize Doctrine collections to paginated responses:
$query = $entityManager->createQuery('SELECT u FROM App\Entity\User');
$users = $responder->paginate($query, $request->query->getInt('page', 1));
Request Handling: Access the RequestStack for dynamic responses:
$request = $responder->getRequest();
$format = $request->getRequestFormat();
Event Listeners: Hook into kernel.response to modify responses globally:
public function onKernelResponse(GetResponseForControllerResultEvent $event)
{
$response = $event->getResponse();
if ($response instanceof JsonResponse) {
$response->headers->set('Content-Type', 'application/vnd.api+json');
}
}
Outdated Dependencies: The package was last updated in 2018. Ensure compatibility with newer Symfony versions (e.g., SerializerComponent changes in Symfony 4+).
config/packages/doze.yaml to match your Symfony version.Missing Normalizers: If using Doctrine collections, ensure app.serializer.normalizer.collection is defined (see README). Without it, pagination may fail silently.
Circular References: The default serializer may struggle with circular references in entities. Configure a custom normalizer:
app.serializer.normalizer.object:
class: Symfony\Component\Serializer\Normalizer\ObjectNormalizer
arguments:
- '@doctrine.orm.entity_manager'
- null
- null
- ['enable_max_depth' => true]
Response Inspection: Use Symfony’s VarDumper to debug serialized data:
$data = $responder->serialize($entity);
dump($data);
HTTP Headers: Verify headers are set correctly (e.g., Content-Type: application/json). Use:
$response = $responder->respond($data);
dump($response->headers->all());
Custom Responder: Extend ResponderService to add domain-specific logic:
class ApiResponder extends ResponderService
{
public function respondWithPagination(array $data, int $page)
{
$data['meta']['pagination'] = ['page' => $page];
return parent::respond($data);
}
}
Middleware Integration: Use Symfony’s HttpKernel to wrap responses:
public function onKernelResponse(FilterResponseEvent $event)
{
$response = $event->getResponse();
if ($response->headers->get('X-API-Version') === null) {
$response->headers->set('X-API-Version', '1.0');
}
}
Testing: Mock ResponderService in tests to isolate logic:
$responder = $this->createMock(ResponderService::class);
$responder->method('respond')->willReturn(new JsonResponse(['data' => []]));
How can I help you explore Laravel packages today?