While going though https://github.com/dingo/api/wiki/Responses#response-builder, it says about
return $this->response->item($user, new UserTransformer);
But where exactly UserTransformer is stored ? I am getting error as Class 'UserTransformer' not found.
Also in which file we have to write custom transformers ?
Thanks
You create a new class, for ease of use extend TransformerAbstract.
—
Sent from Mailbox
On Sat, Jan 17, 2015 at 8:51 PM, Ankur Gokhale [email protected]
wrote:
While going though https://github.com/dingo/api/wiki/Responses#response-builder, it says about
return $this->response->item($user, new UserTransformer);
But where exactly UserTransformer is stored ? I am getting error as Class 'UserTransformer' not found.
Also in which file we have to write custom transformers ?Thanks
Reply to this email directly or view it on GitHub:
https://github.com/dingo/api/issues/341
@ankur0101 Create a new class anywhere in your application it makes logical sense to be. This could be in "app/Transformers/UserTransformer" or in the root of your application (same directory as "vendor" and "app" are located), "src/Foo/Transformers/UserTransformer". It is entirely dependant on the structure of your codebase.
With regards to the Transformer, an example of a UserTransformer would be:
use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract
{
/**
* Turn this item object into a generic array
*
* @return array
*/
public function transform(User $user)
{
return [
'id' => (int) $user->id,
'name' => $user->name,
'phone' => $user->phone,
'email' => $user->email,
'group' => $user->group,
'website' => $user->website,
];
}
}
As @bweston92 mentioned, extend "TransformerAbstract" provided by Fractal (https://github.com/thephpleague/fractal).
That didnt work out. Still gives Class 'UserTransformer' not found.
I am using
return $this->response->item($user, new UserTransformer);
in AuthController for testing.
Where is your "UserTransformer" class located and is it within a namespace?
If it is within a namespace, you will need to add use My\Namespace\UserTransformer above your AuthController.
You will need to declare the class or namespace in your composer.json.
In that "logical" namespace it may be best to version transformers too.
"My\App\Transformers\V1\EloquentUserTransformer"
—
Sent from Mailbox
On Sun, Jan 18, 2015 at 1:35 PM, David Bonner [email protected]
wrote:
Where is your "UserTransformer" class located and is it within a namespace?
If it is within a namespace, you will need to adduse My\Namespace\UserTransformerabove your AuthController.You will need to declare the class or namespace in your composer.json.
Reply to this email directly or view it on GitHub:
https://github.com/dingo/api/issues/341#issuecomment-70408515
Could you please explain more in details ?
@ankur0101 where is your transformer located?
Here are two examples of how to set up your Transformer.
Location: app/transformers/UserTransformer
Class:
use App\User;
use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract
{
/**
* Turn this item object into a generic array
*
* @return array
*/
public function transform(User $user)
{
return [
'id' => (int) $user->id,
'name' => $user->name,
'phone' => $user->phone,
'email' => $user->email,
'group' => $user->group,
'website' => $user->website,
];
}
}
Then within the "classmap" array in your composer.json, add 'app/transformers':
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/transformers"
]
},
Your AuthController will then look similar to this (note the use of the use keyword).
<?php
use UserTransformer;
class AuthController extends Controller { //... }
Location (with @bweston92's versioning suggestion): app/MyApp/Transformers/V1/UserTransformer
Class:
<?php namespace MyApp/Transformers/V1;
use App\User;
use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract
{
/**
* Turn this item object into a generic array
*
* @return array
*/
public function transform(User $user)
{
return [
'id' => (int) $user->id,
'name' => $user->name,
'phone' => $user->phone,
'email' => $user->email,
'group' => $user->group,
'website' => $user->website,
];
}
}
Then using "psr-4", in composer.json, add the namespace:
"autoload": {
"psr-4": {
"MyApp\\": "app/MyApp"
},
},
Your AuthController will then look similar to this (note the use of the use keyword).
<?php
use MyApp/Transformers/V1/UserTransformer;
use Illuminate\Routing\Controller;
use Dingo\Api\Routing\ControllerTrait;
class AuthController extends Controller
{
use ControllerTrait;
//...
}
After implementing either of these, run composer dump-autoloadand the php artisan dump-autoload.
More info on namespacing can be found here:
Lots of helpful information here that should hopefully guide you on how to use transformers. As mentioned, make sure you refer to the Fractal documentation for more information on transformers.
@dbonner1987 Thanks. That helped.
THX,That help me out
@dbonner1987 i think you forgot to put use App\User or to define the arguments passed on transform User. Thanks for this, really helped me.
@brickgale you are correct. Thanks.
Comment has been updated.
@dbonner1987 you are really patient and that helped me, thanks
Most helpful comment
@ankur0101 Create a new class anywhere in your application it makes logical sense to be. This could be in "app/Transformers/UserTransformer" or in the root of your application (same directory as "vendor" and "app" are located), "src/Foo/Transformers/UserTransformer". It is entirely dependant on the structure of your codebase.
With regards to the Transformer, an example of a UserTransformer would be:
As @bweston92 mentioned, extend "TransformerAbstract" provided by Fractal (https://github.com/thephpleague/fractal).