-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Role/scope based result in transformers? #327
Comments
I'm doing the exact same thing. |
In my application-scenario I would use different Transformers in my Controller. E.g., there would be a Furthermore, you would need to inject the |
Have the same issue... Is there any beautiful solution? |
@hegoku , I would suggest to use different e.g, like this: abstract class AbstractProjectTransformer extends Transformer {
$availableIncludes = ['your', 'custom', 'includes'];
includeYour(Project $project) {
// do some stuff here, like
return $this->collection($project->something(), new WhateverTransformer());
}
// other include methods here
}
class UserProjectTransformer extends AbstractProjectTransformer {
transform(Project $project) {
return [
// your custom structure for USER roles here
];
}
}
class ManagerProjectTransformer extends AbstractProjectTransformer {
transform(Project $project) {
return [
// your custom structure for MANAGER roles here
];
}
} The proposed solution is way easier to extend (e.g., add new attributes) than constantly adding new Hope this helps.. all the best |
@johannesschobel This solution is nice, thank you very much! |
@johannesschobel , I used this approach and mixed in some advanced transformation with includes, which adds some nice extra flexibility to my APIs endpoints. BUT.. I am have trouble making this work with collection, which contains multiple types of items (models) and therefor should delegate the transformation of each of the items to a different transformer. To continue with the user example, when including a collection of users with different roles, how do I tell fractal to use a different transformer based on the user's role, and make sure nested includes are passed through? I have been stuck on this problem for days, and can't find a solution. Thanks for anybody's help! |
@yoannisj , what exactly do you mean by
? Do you mean something like this: $data = [
$book1,
$author1,
$book2,
$car,
$user1,
$user2,
// ...
]; Are you actually mixing different resources in one response? if yes, i don't think that this is the desired behaviour or how you should implement it.. Usually you have one "root" resource (e.g., determined by the URL, Hope this helps |
@johannesschobel, I am building an API for a website which contains listing/index pages, including a list of entries (a.k.a. posts). The endpoint to that page $data = [
"title" => "Some listing page",
"url" =>"https://www.example.com/some-listing-page",
"postDate" => "2019-05-08T12h30"
"intro" => "Lorem ipsum dolor…",
"entries" => [
$newsItem, $article, $article, $event, $newsItem, $article, /* ... */
],
/* ... */
]; The "root" transformer for that page loads the data for the "entries" key, through an The examples I have seen in the documentation, seem to suggest that I can only pass a single transformer class to the collection returned in the page transformer's public function includeEntries( $page )
{
$entries = new Query('entries')->parentPage($page)->fetch();
return $this->collection($entries, new EntryTransformer());
} But that would mean, that the Ideally, there would be a way to tell Fractal to:
Is that possible, and how can I achieve such behaviour? Maybe the answer is to rely on a common Sorry, this starts to look more and more like a stack overlfow ticket, rathat than a github issue. I would be happy to re-iterate my question in a more appropriate channel. Thanks a lot for the help! |
#Seems to be a duplicate of #244 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 4 weeks if no further activity occurs. Thank you for your contributions. |
While using Fractal, I want to base my output on the given scopes or roles from logged in users. My question is, can this be done inside the transformer? Of course, in my workflow it can be done, but I'm not sure if it follows the 'rules' of Fractal.
Example code would be:
Thanks in advance!
The text was updated successfully, but these errors were encountered: