Replies: 1 comment 1 reply
-
|
Thank you! This sounds great and I want to implement it. Let me know if you would like to help out. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Description
Proposal to add support for declaring transformers via PHP 8 attributes on Eloquent models (or controllers/resources) instead of relying on explicit transformer registration. This will improve readability, reduce boilerplate, and make it easier to maintain mappings between models and their transformers in large APIs.
Motivation
Currently transformers are often registered or resolved through separate configuration files or in controllers/resources using explicit calls. Using PHP 8 attributes to annotate models (or controller actions) with their transformer class makes the relationship explicit, colocated, and easier to discover. This small syntactic improvement increases developer experience and reduces the surface for mistakes when working with many models and transformers.
Benefits
Proposed behaviour
Example attribute definition:
Model example:
Controller example:
Implementation notes
Backward compatibility and migration
Security and performance considerations
Suggested API / Code locations
Minimal example of resolver (concept):
Request
Please consider adding support for PHP 8 attributes to declare transformers and expose a small resolver integration (configurable) so that response serialization can leverage the annotated transformer when available.
If agreed, I can prepare a small PR with:
Additional questions
Do you prefer the attribute to be placed on Models only, or also allow Controllers to be annotated?
Any preferred attribute namespace or naming convention? Current suggestion: Dingo\Api\Transformer\TransformedBy
Call to action
If you like this idea or want to help, please react/comment below, share suggestions, or volunteer to review/implement the PR — contributions are more than welcome! 🙌✨
Beta Was this translation helpful? Give feedback.
All reactions