alexanevsky/output-normalizer-bundle
[EntityToId] to new versions without breaking existing clients).deleted_at for public-facing APIs).EntityToId for relationships).#[Groups]) or JSON:API metadata.*"This package lets us standardize API responses without sacrificing flexibility. Instead of manually serializing data in every controller—wasting dev time and risking inconsistencies—we’ll define clean, reusable output rules (like DTOs) that automatically shape responses. For example:
json_encode($entity) with declarative schemas.*"This is a lightweight alternative to Symfony Serializer for Doctrine apps, with key advantages:
UserOutput), making it explicit and IDE-friendly.camelCase → snake_case (e.g., userName → user_name).#[EntityToId]).Phone object as +123456? Just implement ObjectNormalizerInterface.Trade-offs:
Proposal:
json_encode in controllers with OutputNormalizer.Example win:
// Before: Controller bloated with serialization logic
return json_encode([
'id' => $user->getId(),
'name' => $user->getFullName(), // Business logic in controller
'city' => $user->getCity()->getId(), // Manual ID extraction
]);
// After: Declarative and DRY
return $this->outputNormalizer->normalize($user, UserOutput::class);
UserOutput.php:
class UserOutput implements OutputInterface {
public int $id;
public string $name;
#[EntityToId] public City $city; // Auto-extracts city ID
}
```"*
How can I help you explore Laravel packages today?