Comments (6)
In my own projects I usually have a one to one mapping with models and transformers, and if I want to expose different fields based on different criterias I would add some conditionals to the transform()
method in the transformer. So that's basically the reasoning. However, I appreciate that others might have a need for this and will look into an elegant way of handling it.
Do you have a concrete example of where it would make sense to have multiple transformers for one model?
from laravel-responder.
It may be a bit of personal preference but I've taken the approach of only sending attributes which are immediately necessary through my JSON responses and consequentially my use cases are quite varied. More concretely, I may want to respond with users' id's and usernames in one case where in another location, (or for a more privileged end-user), I may want to also send first name, last name, and email attributes.
from laravel-responder.
Yeah, that makes sense. I'm experimenting with a few options now. What do you think of being able to return an associative array in the transformer()
method on the models. So for instance a User model could have 3 different transformers:
public static function transformer()
{
return [
'admin' => AdminUserTransformer::class,
'moderator' => ModeratorUserTransformer::class,
'regular' => RegularUserTransformer::class
];
}
Then I haven't decided for a good API to choose which transformer to use. Since there are already quite a lot of (optional) parameters on the success()
method, I'm considering the ability to be able to chain a transformer()
method on the responder:
Responder::transformer('admin')->success($users, 200);
In that case an exception would be thrown if you don't specify the transformer type. Not completely satisfied with it, I'll think about it some more over the weekend.
If you have a better suggestion, please let me know :)
from laravel-responder.
I've been experimenting some more, and decided to allow an alternative syntax to build your success responses. A bit more similar to how the spatie/laravel-fractal package works, the above example would look like this:
Responder::transform($users, 'admin')->respond(200);
You can then also be explicit and instantiate a transformer directly:
Responder::transform($users, new UserTransformer)->respond();
The success()
method is also getting refactored to call on these methods behind the scene, and will basically work as a shorthand.
This alternative syntax will also allow for some control:
Responder::transform($users, 'admin')->serialize(new DataArraySerializer)->respond();
Responder::transform($users, 'admin')->respond(201, [ 'meta' => 'data' ]);
Responder::transform($users, 'admin')->toArray();
Responder::transform($users, 'admin')->toResource();
Implementing this means a bit reworking of a few core pieces of the package, but hopefully shouldn't take too long. I'll keep you posted.
from laravel-responder.
Thought I'd let you know the status of the issue. Got my hands quite full with work related stuff at the moment, but working on a major rewrite of the package to allow a more flexible way of building responses. Quite similar to the code examples above.
I'm guessing it should be done by the end of the week, perhaps a few days longer.
from laravel-responder.
Hey! Sorry for the delay, ended up being a bit more work than expected. A few parts of the package has been completely rewritten from scratch, for the better anyway. I've also gone through the documentation word for word and simplified it quite a bit. You can update to the latest version (v1.2.0+) using composer update
.
You can read about the new API for explicitly transforming models here: https://github.com/flugger/laravel-responder#transformers.
Would love to hear what you think and wether or not it solves all your needs :)
from laravel-responder.
Related Issues (20)
- Transformer mappings circular dependencies
- php 8 support HOT 1
- ConvertToSnakeCase throwing an "Undefined function" error
- error HOT 1
- Transforming To Camel Case in Lumen
- How do I use getResourceKey() on Relationship?
- Transformer not applied HOT 1
- Include message in success response HOT 1
- Laravel Responder installation in Laravel 9 is not working. HOT 11
- Fractal 0.20.0 supprot HOT 3
- I wish there was an *except* method
- Laravel 10 support HOT 7
- composer require flugger/laravel-responder error laravel 10 HOT 4
- Laravel 10 support!
- Different types in NoopSerializer HOT 9
- Wrong Model Namespace on executing the transformer command
- Support for simplePaginate
- Language files folder
- Flugg\Responder\Serializers\NoopSerializer error for method null HOT 3
- Laravel 11 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-responder.